Is there a way to restart pm2 process using cron but only if it's not already running?

The better way of doing it is use the pm2 startup command

http://pm2.keymetrics.io/docs/usage/startup/

To get the automatically-configured startup script for your machine you need to type this command:

# Detect available init system, generate configuration and enable startup system
pm2 startup

You can specify the platform you use by yourself if you want to (where platform can be either one of the cited above):

pm2 startup [ubuntu | ubuntu14 | ubuntu12 | centos | centos6 | arch | oracle | amazon | macos | darwin | freebsd | systemd | systemv | upstart | launchd | rcd | openrc]

The output of this command can be a recommendation of the line to copy/paste with all environment variables and options configured for you.

Example:

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

You simply have to copy/paste the line PM2 gives you and the startup script will be configured for your OS.

Once you run the sudo pm2 startup. It will create the systemctl service. You can check the status of the same using

systemctl status pm2-root

By default the service is not configure to restart automatically. You will run the below commands

sudo mkdir -p /etc/systemd/system/pm2-root.service.d

and then create a file name 10_auto_restart_pm2.conf with below content

[Service]
Restart=always
RestartSec=3

After that execute

systemctl daemon-reload
systemctl restart pm2-service

Now let's test the auto restart part

$ systemctl status pm2-root.service
● pm2-root.service - PM2 process manager
   Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled)
  Drop-In: /etc/systemd/system/pm2-root.service.d
           └─10_auto_restart_pm2.conf
   Active: active (running) since Wed 2018-02-28 16:52:19 UTC; 11s ago
     Docs: https://pm2.keymetrics.io/
  Process: 5014 ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill (code=exited, status=0/SUCCESS)
  Process: 5022 ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)
 Main PID: 5031 (PM2 v2.10.1: Go)
    Tasks: 9
   Memory: 24.3M
      CPU: 460ms
   CGroup: /system.slice/pm2-root.service
           └─5031 PM2 v2.10.1: God Daemon (/home/vagrant/.pm2)

Now we kill the process manually and wait for 3 seconds

$ kill -9 5031
$ sleep 3
$ systemctl status pm2-root.service
● pm2-root.service - PM2 process manager
   Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled)
  Drop-In: /etc/systemd/system/pm2-root.service.d
           └─10_auto_restart_pm2.conf
   Active: active (running) since Wed 2018-02-28 16:52:55 UTC; 641ms ago
     Docs: https://pm2.keymetrics.io/
  Process: 5057 ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill (code=exited, status=0/SUCCESS)
  Process: 5081 ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)
 Main PID: 5088 (PM2 v2.10.1: Go)
    Tasks: 9
   Memory: 24.3M
      CPU: 461ms
   CGroup: /system.slice/pm2-root.service
           └─5088 PM2 v2.10.1: God Daemon (/home/vagrant/.pm2)

As you can see the process/service was restarted automatically. No cron needed and it is how you should do it.


What you're looking to do is start any stopped apps without incurring downtime. A good solution is to use the pm2 startOrReload command. This will start any stopped apps, and will reload any current apps without incurring downtime.

You'll need a config file for the command. if you don't currently have one you can create it using pm2 ecosystem. Make sure it points to app.js.

Then run this command in your cron job:

pm2 startOrReload <your ecosystem file>

See pm2 -h, pm2 startOrReload -h, and pm2 ecosystem -h for more options.


Instead of launching the pm2 process within the cron, launch a bashscript which checks if the pm2 is already running and restarts it if it's not the case.

Edit

Try following (it might be that the pgrep expression needs to be adjusted, I don't know the exact name of the pm2 process):

#!/bin/bash

pID=$(pgrep -x "pm2") 

if [ -n "${pID}" ];
then
    #do nothing 
    echo $pID "already running. not restarting." 
else
    # start it 
    echo "restarting"
    # put your command to start your process here
fi

Tags:

Node.Js

Pm2