Skip to content

Removing PM2 enabled Node.js App

Published: | 2 min read

Table of contents

Open Table of contents

Intro

There are plenty of good tutorials how to run your Node.js application on linux server “forever” with PM2, such as How To Set Up a Node.js Application for Production on CentOS 7 and Running your Node & Express apps forever, no matter what, with Systemd and PM2.

Yet I found none which would tell, how to disable and remove your PM2 enabled application. Here’s how you can do it on CentOS 7 and systemd.

PM2 startup script

In the previous tutorials the reboot surviving service is created with PM2 command sudo pm2 startup systemd after the app itself is running on top off PM2.

This command creates a init script /etc/rc.d/init.d/pm2-init.sh and enables it on several run levels to run the script on server boot. This creates a systemd service, which you can check with sudo systemctl status pm2-init.

This again creates a systemd service, which runs the actual PM2 installed Node.js apps. You can chech this one with sudo systemctl status pm2.

Stopping & disabling services

You can stop the running service(s) simply by running

# stop the service
sudo systemctl stop pm2-init

# disable it from running automatically again
sudo systemctl disable pm2-init

# see status
sudo systemctl status pm2-init

# stop the service
sudo systemctl stop pm2

# disable it from running automatically again
sudo systemctl disable pm2

# see status
sudo systemctl status pm2

After the services have been stopped and disabled, let’s disable them from spawning again at reboot.

Removing the startup automation

In systemd init scripts are managed with chkconfig, see man chkconfigfor more.

First, list the existing scripts with

#
sudo chkconfig --list
# ... should return a line like (among other lines)
pm2-init	0:off	1:off	2:on	3:on	4:on	5:on	6:off

After this confirmation, you can disable it with

sudo chkconfig --del pm2-init

After this, the PM2 will not spawn your apps again after server reboot.

Cleanup

After you have disabled and removed the automation, you can safely remove the app and all of its resources from the server.

I hope this helps someone stuggling to do the same thing.

If you have any further knowledge or tips how to make this better or more complete, or right if you will, please let me know!