jon-hall / pm2-windows-service

Easily install (and uninstall) PM2 as a Windows service
231 stars 66 forks source link

How to keep pm2 running when logging off? #25

Closed abelovic closed 2 years ago

abelovic commented 7 years ago

I have the service running and it re-spawns the process on the server correctly when the server is restarted. The problem is this only works when I am logged into the server. I want this to be kept alive regardless if the user is logged on or not. How can I do this?

App is a meteor app built as a nodejs app

P2_HOME: c:\node\pm2\lib PM2_SERVICE_SCRIPTS: c:/node/pm2/scripts/process.json PM2_SERVICE_PM2_DIR: c:\npm\node_modules\pm2\index.js

process.json

{ "apps": [ { "name": "myapp", "script": "../../apps/myapp/1.0.0/main.js", "log_date_format": "YYYY-MM-DD", "exec_mode": "fork_mode", "watch": true, "env": { "PORT": 3000, "MONGO_URL": "", "ROOT_URL": "http://localhost:3000" }, "env_production" : { "NODE_ENV": "production" } } ] }

jon-hall commented 7 years ago

Hi, apologies for the delay in getting back to you - it took me a little while to find time to do some testing on this. My simple test app (an HTTP server which responds with the current system time and then throws an uncaught exception - simulating a crash - to confirm pm2 is running and able to restart it) is unable to reproduce the issue. If I log out I'm still able to get responses from the server after multiple requests (e.g. multiple restarts by pm2).

This is a substantially simpler test case than a meteor app but indicates that the problem isn't with pm2-windows-service (or with the upstream libraries which would likely be the underlying cause of such a problem - node-windows, or winsw). It seems like maybe your app is unable to run without a logged in user (which could be an issue with meteor, or one of your other runtime dependencies) - I'd advise checking your application logs, the pm2 logs, the pm2-windows-service logs (see the README for where to find them), and also check the windows event logs (though these aren't always very helpful for services hosted by winsw, it's still worth taking a look).

Let me know if you're still not finding anything useful in the logs, or if you find something you think might point to an issue with pm2-windows-service and I'll be happy to help you look into this further.

Orangetronic commented 7 years ago

EDIT: this might be a red herring. starting pm2 as admin seems to cause this error if you then run pm2 status as an ordinary user…

I'm trying to do something really similar — demeteorized meteor app, and I'm seeing the service try to spin up and then fail immediately. So when I try to install the service, or if I run a pm2 save to kick off a service restart, the service tries to start up then falls over.

I get this from my pm2 log, so I reckon it's a permissions issue — possibly not an issue with pm2-windows-service OR meteor, which is good news!

2017-02-15 11:17:20: Error: EPERM: operation not permitted, open 'D:\pm2_processes\pm2.pid'
    at Error (native)
    at Object.fs.openSync (fs.js:549:18)
    at Object.fs.writeFileSync (fs.js:1156:15)
    at Daemon.innerStart (C:\nvm\v4.6.0\node_modules\pm2\lib\Daemon.js:87:8)
    at Domain.<anonymous> (C:\nvm\v4.6.0\node_modules\pm2\lib\Daemon.js:60:10)
    at Domain.run (domain.js:228:14)
    at Daemon.start (C:\nvm\v4.6.0\node_modules\pm2\lib\Daemon.js:59:5)
    at Object.<anonymous> (C:\nvm\v4.6.0\node_modules\pm2\lib\Daemon.js:398:10)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
jon-hall commented 7 years ago

@Orangetronic Sorry it took me so long to reply on here, I believe that you may be right that your issue is related to the issues mentioned in the caveats section.

The error which came up when trying to run pm2 status from a non-admin prompt (after the service had started it as the service user) used to be somewhat different from that, so I'll have to re-check this using the latest version of pm when I get a chance.

If you are still having issues and believe something may be going on which isn't described in the caveats, then please post back here with any more errors/logs you have and, if possible, some basic steps I might be able to reproduce the issue (it sounds like creating an empty meteor app and trying to run it using the service might be a way to recreate).

Orangetronic commented 7 years ago

@jon-hall thanks for getting back to me — no worries on the delay!

I've followed the setup instructions on a blank windows VM, and everything worked perfectly.

I reckon the service failing immediately is down to the permissions on the machine I'm targeting, but I'm not really well enough versed in Windows to debug. I don't think it's an issue with pm2-windows-service though! 😁

shashi97 commented 6 years ago

@jon-hall

i'm using pm2-windows-service with pm2, When user being log off my site is gone for offline and when i take server again and logging than that start automatically. Is there any command or something to make that process always running.

@ Window server 11 thanks

jon-hall commented 6 years ago

@shashi97 the service should stay up after logging out; I've only been able to verify locally on Windows 10, but the service stays up after logging out and the site it's running continues to respond (node@8.4.0, npm@5.4.2, pm2@2.0.19, pm2-windows-service@0.2.1).

Have you checked the errors logs for pm2 (in PM2_HOME) and the service itself (in the src/daemon dir of this package's install location)? There might be something useful in one of those.

If there's no errors in the logs, check the service configuration - is it running under the Local System user and configured to start automatically?

Let me know if you find anything from the above, and if not I'll try and see if there's anything else I can think of that might be causing it to stop when you log out.

shashi97 commented 6 years ago

hello @jon-hall the issue is with when server get restarted if no user log in on server then it will not started the app automatically when a user enter password in server then pm2 started.

it means if server is on and no user appeared to log in in server app will not come to online

jon-hall commented 6 years ago

@shashi97 I've confirmed on my setup that, after restarting my machine, the pm2 service starts up without having to log a user in, and I get responses from the node app being run by the pm2 service.

This issue is going to be something specific to your setup, I can try and help debug but you'll need to provide me with the information requested in my last post in order for me to be able to figure out what might be going wrong (pm2 logs, application logs, service daemon logs, service setup info, server information etc.).

pat-flew commented 6 years ago

Just a quick report - I was having the same issue but it went away after installing latest versions of everything (node, npm, pm2, pm2-windows-service) and restarting. Previously the app processes stopped responding on logout, and I had to manually resurrect them or restart the windows service when logging back in.

jon-hall commented 6 years ago

@pat-flew Thanks for posting that a full refresh fixed it in your case - I've not being able to reproduce the issue and so haven't been able to find a cause or a fix; so for anyone else getting this issue it's probably worth giving a reinstall of everything a try since it might fix it.

Tibasa commented 4 years ago

@shashi97

I found the solution. You just need to access services.msc, right-click on the service, select properties, go to logon tab and check the option "allow service to interact with desktop".

mohan570 commented 3 years ago

@shashi97

I found the solution. You just need to access services.msc, right-click on the service, select properties, go to logon tab and check the option "allow service to interact with desktop".

it's not working

jon-hall commented 2 years ago

Closing this as the package is now deprecated, users should migrate to pm2-installer.