Restart pm2 app every 12h with cron
Asked Answered
U

3

29

Tried this, but it doesn't work:

SHELL=/bin/bash
PATH=/usr/lib/node_modules/pm2/bin
* 0,12 * * * pm2 restart all

What am I doing wrong?

Unlatch answered 29/5, 2016 at 20:8 Comment(1)
You are removing the other paths from $PATH. Instead, say PATH=$PATH:/usr/lib/node_modules/pm2/bin. Also, you need to mention node's path before pm2 --> */5 * * * * /root/.nvm/v0.11.9/bin/node /root/.nvm/v0.11.9/bin/pm2 restart allEldoree
J
54

Edit cron with crontab -e and add the following:

0 */12 * * * /usr/bin/node /usr/bin/pm2 restart all

For the schedule, use 0 */12 * * * for every 12 hours, or 0 0,12 * * * for 0:00 and 12:00 specifically. (Your schedule, * 0,12 * * *, would trigger every minute of hour 0 and hour 12, 0:00, 0:01, 0:02...)

For the command, as fedorqui mentioned, use the path to node followed by the path to pm2 and the pm2 options. Use which node and which pm2 to get the path to node and pm2.

Jahncke answered 31/3, 2017 at 3:48 Comment(0)
G
11

Among multiple restart strategies, PM2 can restart application based on a cron format via the option --cron-restart

Restart app every midnight:

pm2 start app.js --cron-restart="0 0 * * *"

For more information check out the doc:

https://pm2.keymetrics.io/docs/usage/restart-strategies/#restart-at-cron-time

Gambrell answered 26/6, 2021 at 2:9 Comment(1)
This doesn't work if there's a process.exit(1) in the script. :(Shupe
F
4

Don't restart, reload (zero down-time)

Reload myApp every day at 4:30 AM

30 4 * * * /usr/local/bin/node /usr/local/bin/pm2 reload <myAppId> > /dev/null 2>&1

To check the full path of node and pm2 do which node and which pm2. The portion > /dev/null 2>&1 ignores the stdout and stderr.

Fara answered 4/11, 2021 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.