Pm2: Probleme with Stopping & Starting PM2 thanks to CRON

Created on 5 Dec 2016  Â·  5Comments  Â·  Source: Unitech/pm2

I’m using the PM2 process manager to start (pm2 start process.yml) and stop (pm2 delete all) several StorjShare-CLI nodes (where process.yml is a configuration file for PM2 where all my nodes are defined).
I’m now trying to automatically start and stop theses processes once a day thanks to cron …

… but I encountered issues.

My /etc/crontab look like

55 11 * * *     babonet13   echo "PM2 Stop & Start StorjShare-CLI processes" > /home/babonet13/cron-log.txt
00 12 * * *     babonet13   /home/babonet13/.nmv/versions/node/v6.9.1/bin/node /home/babonet13/.nmv/versions/node/v6.9.1/lib/node_modules/pm2/bin/pm2 delete all
05 12 * * *     babonet13   /home/babonet13/.nmv/versions/node/v6.9.1/bin/node /home/babonet13/.nmv/versions/node/v6.9.1/lib/node_modules/pm2/bin/pm2 start /home/babonet13/process.yml

Only the 55 11 * * * line works ... the 2 others fails !
What I'm doing wrong ?

Best Regards
Bernard

Need More Data Question

Most helpful comment

Here is my suggestion:

1st. ssh to your server, and type vi /var/www/pm2-restart.sh (you can put this shell anywhere), and write with the following shell script:

#!/bin/bash
pm2="$(which pm2)"
# $pm2 flush
$pm2 kill
# clean up logs if necessary, make sure your pm2 home directory is /home/babonet13/.pm2
# rm -rf /home/babonet13/.pm2/logs/*
# update the following path to your work directory
cd /my/node/app/directory/
$pm2 start apps.json

Escape > wq -> Enter

2nd. type crontab -u babonet13 -e to edit babonet13's crontab, write with the following scripts:

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
MAILTO="babonet13"
SHELL=/bin/bash
# restart your processes at 00:15:00 every day.
15 0 * * * /var/www/pm2-restart.sh >> /var/log/pm2.log

Escape > wq -> Enter

All 5 comments

Install pm2 globally (npm install -g pm2) and just use put pm2 delete all in your cron.

PS : please take care of formatting, i edited your post cause it was unreadable.

@vmarchaud thanks for that information. (Sorry for formatting but I've not found how to solve it)

@vmarchaud I just saw in my documentation that I have already make a global installation of MP2 (but not as root, as babonet13 which is the current user). On my Ubuntu installation I could not log me as root, so I suppose I need to make a global MP2 installation using "sudo npm -g mp2" ?

Just put your use crontab -e and write the cron you want, they will be run with the current user.

Here is my suggestion:

1st. ssh to your server, and type vi /var/www/pm2-restart.sh (you can put this shell anywhere), and write with the following shell script:

#!/bin/bash
pm2="$(which pm2)"
# $pm2 flush
$pm2 kill
# clean up logs if necessary, make sure your pm2 home directory is /home/babonet13/.pm2
# rm -rf /home/babonet13/.pm2/logs/*
# update the following path to your work directory
cd /my/node/app/directory/
$pm2 start apps.json

Escape > wq -> Enter

2nd. type crontab -u babonet13 -e to edit babonet13's crontab, write with the following scripts:

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
MAILTO="babonet13"
SHELL=/bin/bash
# restart your processes at 00:15:00 every day.
15 0 * * * /var/www/pm2-restart.sh >> /var/log/pm2.log

Escape > wq -> Enter

Was this page helpful?
0 / 5 - 0 ratings

Related issues

morfies picture morfies  Â·  3Comments

cchan picture cchan  Â·  3Comments

rajendar38 picture rajendar38  Â·  3Comments

webchaz picture webchaz  Â·  3Comments

ghost picture ghost  Â·  3Comments