pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

pm2-init.sh does not work on centos 7

Open vitxd opened this issue 11 years ago • 20 comments

In centos 7 when I reboot the server, pm2 does not load the processes saved on shutdown. The service name (pm2-init.sh) also collides with systemctl format. Can I suggest to rename the file in just pm2?

vitxd avatar Dec 24 '14 12:12 vitxd

It's weird because it works if I power off the machine (unplug the power cable) but not if I run a correct reboot. Can anybody help me?

vitxd avatar Jan 08 '15 10:01 vitxd

Maybe we missed something. I'm not familiar with centos chkconfig, maybe someone could check the init file : https://github.com/Unitech/PM2/blob/master/lib/scripts/pm2-init-centos.sh#L5

soyuka avatar Jan 08 '15 10:01 soyuka

thing is, centos 7 uses systemd instead. It used to work correctly on centos 6 which used chconfig to enable/disable services

vitxd avatar Jan 08 '15 10:01 vitxd

Hi, i came across the same issue while using centos 7,

I am running this under a normal user (not root)

once i commented out the 'delete all' & 'dump' under stop in the initd script my apps kept coming back on reboot.

Azerothian avatar Feb 25 '15 04:02 Azerothian

Hi @Azerothian ,

Thanks a lot for sharing your solution! This indeed fix the problem. I also have change the export PM2_HOME="/root/.pm2" to my normal user home folder.

gregwym avatar Mar 31 '15 03:03 gregwym

@Azerothian worked on fedora =) yay!

pablomaurer avatar Oct 15 '15 10:10 pablomaurer

me too! In centos 7,when reboot the sever,the pm2 startup do not work,but when power off the server,and then restart the server,the pm2 startup work fine.

qinyang912 avatar Oct 21 '15 02:10 qinyang912

Use this with CentOS 7:

sudo pm2 startup systemd

adrianocola avatar Nov 23 '15 17:11 adrianocola

@Azerothian sudo pm2 startup systemd still not work

meteormatt avatar Feb 04 '16 06:02 meteormatt

Feels to me like this needs more than a manual workaround. If we detect centos 7+ in the cli we should be able to do this correctly. Correct? Or there could be an optional version flag on the startup command.

4everinbeta avatar Feb 16 '16 18:02 4everinbeta

I also have problem with the startup pm2 on CentOS 7. It seems that these problems disappear if the application is run under root:

# in project folder
sudo pm2 start index.js

and then:

sudo pm2 startup centos

KostyaTretyak avatar Mar 22 '16 04:03 KostyaTretyak

If on CentOS 7 run pm2 list not under root, showing an empty table (only headers). So should it be?

KostyaTretyak avatar Mar 22 '16 04:03 KostyaTretyak

I played with pm2, and found two working way for CentOS 7 (in your project folder index.js - main file): # 1

sudo pm2 start index.js
sudo pm2 save
sudo pm2 startup centos

# 2

pm2 start index.js
pm2 save
pm2 startup centos
# and run the script generated in the previous code under sudo

NOT working next

pm2 start index.js
pm2 save
sudo pm2 startup centos

and NOT working next

pm2 start index.js
sudo pm2 startup centos

KostyaTretyak avatar Mar 23 '16 13:03 KostyaTretyak

@KostyaTretyak Still not working here.

meteormatt avatar Apr 05 '16 06:04 meteormatt

@meteormatt, but if in your project to do this:

sudo chown $USER: -R .
sudo chmod 755 index.js

KostyaTretyak avatar Apr 05 '16 08:04 KostyaTretyak

Here is what worked for me. (to launch Kibana4 under the node user on a RHEL/CentOS 7 server. As root pm2 startup systemd /home/node/kibana4.yml -u node pm2 save

Note about above: change the -u (user) node to what user you want the app to run as and the path to the startup file of the app.

Now you should be able to reboot and it will run the app as the node user on startup.

Hope this helps. Mark

MarkOStewart avatar Apr 06 '16 15:04 MarkOStewart

Thanks @Azerothian, works like a boss. Just comment out '$PM2 delete all' in /etc/init.d/pm2-init.sh

Danthemanwenttojapan avatar May 06 '16 14:05 Danthemanwenttojapan

Thanks @Azerothian and I runned this command:

  • my user is 'centos'

sudo su -c "env PATH=$PATH:/usr/bin pm2 startup centos -u centos --hp /home/centos"

PedroWilliam avatar Jul 16 '16 23:07 PedroWilliam

Closing in favor of #2559

To try the new startup system:

$ npm install Unitech/pm2#development -g
$ pm2 update
$ pm2 startup     # Then copy / paste the command displayed
$ pm2 unstartup

Unitech avatar Dec 06 '16 11:12 Unitech

I am using Centos 7 pm2 2.1.6 node v6.9.1

The following command worked for me. I am running pm2 as node user not root.

sudo pm2 startup systemd run as sudo, any user pm2 save run as node user, make sure the app is running

update the user to node user in the service file.

cat /etc/systemd/system/pm2.service

Under [Service], u should see the following

[Service] Type=forking User=node

the whole pm2.service file for reference

cat /etc/systemd/system/pm2.service [Unit] Description=PM2 next gen process manager for Node.js After=network.target remote-fs.target

[Service] Type=forking User=node

ExecStartPre=/usr/local/lib/node_modules/pm2/bin/pm2 kill ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all

ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 dump ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 delete all ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill

[Install] WantedBy=multi-user.target

Reboot the server for test.

Check /var/log/messages to verify pm2 started correctly. You would see something like this after reboot.

Dec X 13:29:17 X.X.X.X pm2: [PM2] Spawning PM2 daemon with pm2_home=/home/node/.pm2 Dec X 13:29:17 X.X.X.X systemd: Started Apply the settings specified in cloud-config. Dec X 13:29:17 X.X.X.X systemd: Starting Execute cloud user/final scripts... Dec X 13:29:17 X.X.X.X pm2: [PM2] PM2 Successfully daemonized Dec X 13:29:17 X.X.X.X pm2: [PM2] Resurrecting Dec X 13:29:17 X.X.X.X pm2: [PM2] Restoring processes located in /home/node/.pm2/dump.pm2

P.S. I think pm2 doesn't work well in centos 7 if you are using chkconfig, atleast I couldn't get it working

jpujari-cartera avatar Dec 07 '16 18:12 jpujari-cartera