Is there a way to restart the pm2 process using cron, but only if it is not already running?

I want to restart the Node.Js application using PM2 via

pm2 restart app.js

using crontab, but ONLY if the application is not already running (for example, if my server crashes and restarts, and pm2 does not restart).

the above command restarts it even if it works.

How to fix it?

UPDATE

I do not want my application to restart if it is already running. I want it to restart only if it was indicated as "stopped" or it does not work. Some suggestions suggest writing a bash script, but what would it be? I tried the options below, but they either do not work or restart the application, even if it works.

+6
source share
3 answers

Instead of starting the pm2 process inside cron, run bashscript, which checks if pm2 is working and restarts it if it isn't.

Edit

Try the following (perhaps you need to adjust the pgrep expression, 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
+3
source

The best way to do this is to use the command pm2 startup

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

To get an automatically configured script run for your computer, you need to enter the following command:

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

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

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

/ , .

:

[PM2] root. :       sudo su -c "env PATH = $PATH:/home/unitech/.nvm/versions/ node/v4.3/bin pm2 startup -u -hp

/ , PM2 , script .

sudo pm2 startup. systemctl service. ,

systemctl status pm2-root

.

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

10_auto_restart_pm2.conf

[Service]
Restart=always
RestartSec=3

systemctl daemon-reload
systemctl restart pm2-service

$ 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)

3

$ 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)

, / . cron, .

+4

, , - , . pm2 startOrReload. , .

. , pm2 ecosystem. , app.js.

cron:

pm2 startOrReload <your ecosystem file>

. pm2 -h, pm2 startOrReload -h pm2 ecosystem -h.

+3

All Articles