Krishna Chaitanya
Krishna Chaitanya

Reputation: 31

How to use supervisor to start pm2 process in ubuntu

We have a setup where a nodejs program is being started by the pm2 process manager. And the pm2 process itself is started & being monitored by supervisor process.

My conf looks like below:

[program:node-service]
process_name=%(program_name)s
command=pm2 start /home/deploy/nodeJS/app.js
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/deploy/logs/appjs.log

Expected Outcome:

Supervisor starts PM2 Deamon. PM2 starts node process.

Command: ps aux | grep pm2

root       664  0.8  0.2 1436832 70992 ?       Ssl  13:57   0:02 PM2 v5.2.0: God Daemon (/etc/.pm2)

Command: ps aux | grep node

root     20893  0.5  0.2 1309368 70692 ?       Ssl  14:02   0:01 node /home/deploy/nodeJS/app.js

Actual Outcome:

Supervisor starts PM2 Daemon. PM2 starts node process. Supervisor or pm2 is trying to start node process again.

Command: ps aux | grep pm2

root       664  0.8  0.2 1436832 70992 ?       Ssl  13:57   0:02 PM2 v5.2.0: God Daemon (/etc/.pm2)


root     21259 61.0  0.2 1164964 74040 ?       Rl   14:02   0:00 node /usr/bin/pm2 start /home/deploy/nodeJS/app.js

Command: ps aux | grep node

root     20893  0.5  0.2 1309368 70692 ?       Ssl  14:02   0:01 node /home/deploy/nodeJS/app.js

I couldn't figure out why is it trying to create the other process "node /usr/bin/pm2 start /home/deploy/nodeJS/app.js" is started.

Upvotes: 2

Views: 1143

Answers (2)

AdrianL
AdrianL

Reputation: 1

I know, why is supervisord doing that. The problem is that supervisord expects a monitored process to remain in the foreground, i.e. does not daemonize. However pm2 start <app.js> does just that - it goes into background upon successful execution. Then the supervisord "thinks" that the process went out, and relaunches it. I guess it gives up after like 4 tries. You need to run pm2 in foreground:

pm2 start --no-daemon /home/deploy/nodeJS/app.js

Please let us know, if it helps. I am aware that the --no-daemon switch is available only in some recent versions of pm2.

Upvotes: 0

Cris John Rey Tarpin
Cris John Rey Tarpin

Reputation: 45

are you able to solve this? If not, it's because supervisor don't know where to find all those binaries. In short, you need to load the system path environment first before calling the binaries.

command=/bin/bash -c "source /root/.bashrc && pm2 start /home/deploy/nodeJS/app.js"

and in your .bashrc file you have to export the path of those binaries

EXPORT PATH=/opt/cpanel/ea-nodejs18/bin/:$PATH

/opt/cpanel/ea-nodes18/bin/:$PATH is where my node binaries installed including pm2. If you're not sure how to find it just type the following command and it should give you the path and use that path in your .bashrc

where pm2

Hope this helped.

Upvotes: 0

Related Questions