Hi all,
I may be misunderstanding the cron-restart setting but I _think_ it can be used for scheduling a script to run on a schedule. In my case I want the "updater" to be run every 15 minutes. The script itself runs in a few seconds and it's done. The database connection it uses is shared so, if I don't run process.exit(0) the script just hangs there just fine. I don't mind it staying up listening in that connection but PM2 is not restarting it every 15 minutes. I just want it to run afresh every 15 minutes regardless.
I tried adding process.exit(0) after the task is done but then PM2 immediately restarts it so the script runs over and over again too frequently which is not good.
I tried using setTimeout to trigger a call to process.exit(0) every 15 minutes and that works letting PM2 restart the script immediately but then the cron_restart setting becomes redundant.
Am I misusing this setting? How would you suggest I solve this?
Thank you,
{
"apps" : [
{
"name" : "server",
"script" : "server.js",
"exec_mode" : "cluster",
"instances" : 1,
"ignore_watch": ["node_modules", "logs"]
},
{
"name" : "updater",
"script" : "workers/updater.js",
"exec_mode" : "cluster",
"instances" : 1,
"cron_restart": "*/15 * * * *",
"ignore_watch": ["node_modules", "logs"]
}
],
"deploy" : {
"production" : {
"key" : "./keys/deploy",
"user" : "deploy",
"host" : "...",
"ref" : "origin/master",
"repo" : "[email protected]:....git",
"path" : "/home/deploy",
"post-deploy": "npm i && pm2 startOrRestart ecosystem.json --env production",
"env" : {
"NODE_ENV": "production"
}
}
}
}
I'm sorry but shouldn't 0 */15 * * * run my script every 15 minutes?
Nope, every fifteen hours, it should be:
0 */15 * * * *
# or
*/15 * * * *
@Tjatse Thank you. You're right.
Hi, sorry to hijack this thread, but I was wondering why this configuration makes my script run over and over again?
{
name : "app",
script : "app-script.js",
exec_mode : "cluster_mode",
cron_restart : "0 13 * * * *", //daily at 13:00
env_production : {
NODE_ENV: "production"
}
},
The script terminates with a process.exit(0) if successful and process.exit(-1) if not, but I don't want it to restart every single time. I just want it to run once every day at 13:00...
cron_restart is actually embed inside your process so you'll need it to not exit.
cron_restart does not work at all. This issue has been in discussion from 2014 but till now no resolution or proper documentation. Please remove this feature or say it does not work. Lot of people will be wasting their time on this.
@vinokanth92
I didn't work for me with the version 3.5.1 when I've set only cron_restart setting. But when I followed this answer's instructions ( https://stackoverflow.com/a/54455715/741782 ), I've added autorestart: false (and also watch: false, instances: 1) and after that all further executions (except the first one) happened in time specified in cron_restart setting.
Most helpful comment
cron_restartdoes not work at all. This issue has been in discussion from 2014 but till now no resolution or proper documentation. Please remove this feature or say it does not work. Lot of people will be wasting their time on this.