Search code examples
node.jsexpresspm2windows-server-2016

Keeping node.js express running after logoff


I've setup a node.js app started with pm2 and all is working fine

I've also tried adding watch and save options (see more about the windows service below)

pm2 start app.js --watch
pm2 save

but eventually the app is no longer running after I logoff from Windows RDP.

Below the relevant output from pm2 report

==
PM2        | 2021-04-10T11:26:39: PM2 log: --- New PM2 Daemon started ----------------------------------------------------
PM2        | 2021-04-10T11:26:39: PM2 log: Time                 : Sat Apr 10 2021 11:26:39 GMT+0200 (Central European Summer Time)
PM2        | 2021-04-10T11:26:39: PM2 log: PM2 version          : 4.5.6
PM2        | 2021-04-10T11:26:39: PM2 log: Node.js version      : 14.16.1
PM2        | 2021-04-10T11:26:39: PM2 log: Current arch         : x64
PM2        | 2021-04-10T11:26:39: PM2 log: PM2 home             : V:\.pm2
PM2        | 2021-04-10T11:26:39: PM2 log: PM2 PID file         : V:\.pm2\pm2.pid
PM2        | 2021-04-10T11:26:39: PM2 log: RPC socket file      : \\.\pipe\rpc.sock
PM2        | 2021-04-10T11:26:39: PM2 log: BUS socket file      : \\.\pipe\pub.sock
PM2        | 2021-04-10T11:26:39: PM2 log: Application log path : V:\.pm2\logs
PM2        | 2021-04-10T11:26:39: PM2 log: Worker Interval      : 30000
PM2        | 2021-04-10T11:26:39: PM2 log: Process dump file    : V:\.pm2\dump.pm2
PM2        | 2021-04-10T11:26:39: PM2 log: Concurrent actions   : 2
PM2        | 2021-04-10T11:26:39: PM2 log: SIGTERM timeout      : 1600
PM2        | 2021-04-10T11:26:39: PM2 log: ===============================================================================


Please copy/paste the above report in your issue on https://github.com/Unitech/pm2/issues

I wonder whether the pm2 home path could be the root cause, since it is a user share, but I've found how to change it on windows via the PM2_HOME env var and it doesn't do the trick either.

I've searched the github issues and it seems there are already similar open issues there, for example this one:

Same happens if I go directly to server start pm2 and logoff user, all processes will be gone. Is there a way to start pm2 on widows globally so it doesn't exit when user is logging out ? Or I am doing something wrong ?

and

I would also like to understand if there is any option to keep Apps running after logging off the user from Windows serve

Anyone has a solution here?

I've also tried to setup the pm2-windows-service with admin cmd as describe in the readme here.

And I've added a handler for SIGTERM in the express app:

app.listen(HTTP_PORT, () => {
    console.log("Server is listening on port " + HTTP_PORT);
});

process.on('SIGINT', function() {
    console.log('Do not shut down the app on user log-off');
    //server.close();
  });

Solution

  • There was a strane final space in the PM2 home path and in the app path and that folder was locked when I tried to rename it in the explorer (still "locked" after many reboots... finally deleted using this special syntax). Changed the space with underscore, I've restarted the virtual machine and issued a

    pm2 reloadLogs
    pm2 report
    

    Finally I restarted and saved the app again via pm2.

    Before pm2 save, notice that is very important the comment outputted from pm2-service-install in your admin cmd prompt.

    PM2_HOME value (this path should be accessible to the service user and should not contain any "user-context" variables [e.g. %APPDATA%]): F:\Appl\pm2

    After pm2-service-install you still need to manually start the service the first time. Furthermore set "Log on as Local System account" and mark "Allow service to interact with desktop".

    Signed out and rebooted the virtual machine again to double check: the pm2 app is running fine now (I have to use and admin cmd with pm2 though).

    In case you deploy updates needing a new environment variable, refresh the variables with

    pm2 restart 0 --update-env
    pm2 save
    

    don't forget to save after the --update-env!

    check your app (e.g. id 0) with

    pm2 env 0
    

    In such a case keep in mind that you can do a quick check/test by restarting the PM2 service (or, of course, with a machine reboot).