Streamlining PM2 Startup for Node.js Applications: A Comprehensive Guide
Tags: node, pm2, autorestart, webdev
Published: 2024-02-27 08:02:22

Ensuring the continuous operation of Node.js applications is essential for maintaining their availability and reliability. PM2, a leading process manager for Node.js applications, offers a streamlined solution for automating the startup process, enabling applications to persist across system reboots and failures. In this guide, we'll explore the steps to set up PM2 for automatic startup, covering various init systems and customisation options.

**Supported Init Systems:**
PM2 boasts compatibility with a diverse range of init systems:

- systemd
- upstart
- launchd
- openrc
- rcd
- systemv

**Supported OS:**

- systemd: Ubuntu >= 16, CentOS >= 7, Arch, Debian >= 7
- upstart: Ubuntu <= 14
- launchd: Darwin, MacOSx
- openrc: Gentoo Linux, Arch Linux
- rcd: FreeBSD
- systemv: Centos 6, Amazon Linux

**Generating a Startup Script:**

```
pm2 startup
```
After executing this command you'll get bunch of lines printed on terminal, you need to look for following lines

```
[PM2] You have to run this command as root. Execute the following command:
      sudo su -c "env PATH=$PATH:/home/unitech/.nvm/versions/node/v14.3/bin pm2 startup <distribution> -u <user> --hp <home-path>
```

Now copy the generated command and run with Sudo,"**distribution, user, and home-path**" will get automatically replaced with the respective values. If you wish to run it from different user, you can change those values. Following is example command. 

```
sudo env PATH=$PATH:/home/ec2-user/.nvm/versions/node/v16.19.1/bin /home/ec2-user/.nvm/versions/node/v16.19.1/lib/node_modules/pm2/bin/pm2 startup systemd -u ec2-user --hp /home/ec2-user
```
(Don't copy this command from above code box, copy what is generated on your terminal, this machine specific command)

With above command you've made PM2 will start automatically after restart or if you generate new servers from AMI. 

Now if your Node.JS application isn't already started, start your application, 

```
PM2 start path to your start up file --name app name

```
It'll look like something like this, 

![PM2 list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/haht6gwgl6buvw1srd9u.png)
**Saving the App List:**
After initiating the desired applications, safeguard them against system restarts:

```
pm2 save
```

**Manual Process Resurrection:**
Manually restart previously saved processes:

```
pm2 resurrect
```
**Disabling Startup System:**
Temporarily disable or remove the current startup configuration:
```
pm2 unstartup
```
**Updating Startup Script:**
Incorporate changes to the Node.js version:

```
pm2 unstartup
pm2 startup
```
**SystemD installation checking**

```
systemctl list-units
```
This will output all systemctl users, you need to look for pm2 user entry in the list. If you find the user in the list, then you've successfully added the PM2 to be started automatically after restart. 

By following these streamlined steps and utilizing the provided commands, developers can automate PM2 startup processes efficiently, ensuring the seamless and persistent operation of their Node.js applications across diverse environments.