Pm2: Startup for systemd fails if user is not root

Created on 24 Jul 2015  ยท  4Comments  ยท  Source: Unitech/pm2

Hi,
I'm experiencing some problems with the "startup" feature for systemd. It fails to start the service correcty when pm2 is needs to be executed from a user different than root. I think I can explain the problem as a missing .pm2 folder and missing dump for the user. Please follow PR https://github.com/Unitech/PM2/pull/333 as a reference of what happens.

The command it executes is:

su -c "pm2 dump && pm2 kill && systemctl daemon-reload && systemctl enable pm2 && systemctl start pm2"

In a brief, the command to create the systemd script needs to be executed as root, since it must write to /etc/systemd folder. The problem is that when it executes "pm2 dump", it dumps in root's home, creating "/root/.pm2/dump.pm"

But later, when it tries to start the service with "systemctl start pm2", the service is started as user "user", which is missing the dump in its own folder (ex: /home/user/.pm2/dump.pm).

# pm2 startup systemd
[PM2] Spawning PM2 daemon
[PM2] PM2 Successfully daemonized
[PM2] You have to run this command as root. Execute the following command:
      sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u user
# sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u user
[PM2] Generating system init script in /etc/systemd/system/pm2.service
[PM2] Making script booting at startup...
[PM2] -systemd- Using the command:
      su -c "pm2 dump && pm2 kill && systemctl daemon-reload && systemctl enable pm2 && systemctl start pm2"
Command failed: [PM2][WARN] No process found
Job for pm2.service failed. See "systemctl status pm2.service" and "journalctl -xe" for details.
# sudo systemctl status pm2
โ— pm2.service - PM2 next gen process manager for Node.js
   Loaded: loaded (/etc/systemd/system/pm2.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2015-07-24 13:23:08 UTC; 58s ago
  Process: 2407 ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=1/FAILURE)
 Main PID: 2019 (code=exited, status=0/SUCCESS)

Jul 24 13:23:07 box systemd[1]: Starting PM2 next gen process manager for Node.js...
Jul 24 13:23:08 box pm2[2407]: [PM2] Resurrecting
Jul 24 13:23:08 box pm2[2407]: [PM2] No processes saved; DUMP file doesn't exist
Jul 24 13:23:08 box systemd[1]: pm2.service: control process exited, code=exited status=1
Jul 24 13:23:08 box systemd[1]: Failed to start PM2 next gen process manager for Node.js.
Jul 24 13:23:08 box systemd[1]: Unit pm2.service entered failed state.
Jul 24 13:23:08 box systemd[1]: pm2.service failed.

This is exactly what happens if I try to resurrect from "user" with a missing dump. Notice the exit status of 1.

# pm2 resurrect
[PM2] Resurrecting
[PM2] No processes saved; DUMP file doesn't exist
user@box:~$ echo $?
1
Startup Bug

All 4 comments

After I bit of experimenting, I think the issue can be solved by this command instead:

su user -c "pm2 dump && pm2 kill" && su root -c "systemctl daemon-reload && systemctl enable pm2 && systemctl start pm2"

This allows to create a dump in the user folder, then switch to root and create the systemd files. When systemd starts pm2, it will to it as the user which will find the dump where exected.
Please let me know if you agree and want a PR for this.

Thanks!

su user -c "pm2 dump && pm2 kill" && su root -c "systemctl daemon-reload && systemctl enable pm2 && systemctl start pm2"

:+1: pm2 kill isn't really needed though

A PR would be welcome! Thanks!

I agree, it is not required. I just left it since it was present in the original submission. I already ranted a bit here https://github.com/Unitech/PM2/issues/1460 regarding pm2 starting the daemon even when not required. :-)
This is one example: dump shoud check if daemon is running and if it is, just dump. If it is not, it should start, dump, then stop. As it is, we have 50% of leaving the state different from what we found. It is not a big problem, though.

I'll work on the PR, thanks!

Done, enjoy!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexpts picture alexpts  ยท  3Comments

phra picture phra  ยท  3Comments

xzycn picture xzycn  ยท  3Comments

ghost picture ghost  ยท  3Comments

cchan picture cchan  ยท  3Comments