pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

pm2 cron schedule starts immediately, but it should start next morning instead

Open smartini87 opened this issue 3 years ago • 6 comments

What's going wrong?

I set up the cron job like "*pm2 start index.js --cron '00 06 * * ' --no-autorestart" at 4pm and I expected pm2 to start the next morning at 6AM, but it immediately starts the node script. Is this intended that it "knows" it's after 6AM so just start it?

How could we reproduce this issue?

"pm2 start index.js --cron '00 06 * * *' --no-autorestart"

Supporting information

I use pm2 5.2.2 on my newest Raspberry Pi (v4) version (standard OS)

smartini87 avatar Nov 02 '22 16:11 smartini87

Using the --cron option tells it at what time you want it to restart, not what time you want it to start at.

jackchristel avatar Nov 06 '22 05:11 jackchristel

Thank you for the explanation. Is there a possibility to add 'beginn cron job at' as a new feature?

smartini87 avatar Nov 06 '22 20:11 smartini87

Same question

koplenov avatar Mar 29 '23 11:03 koplenov

Came here with the same issue. There should be an option that prevents the initial run from occurring, e.g. "just schedule, do not run now".

esetnik avatar Dec 19 '23 20:12 esetnik

there is no option for this as far as I know. But I have added the following condition to index.js itself when I had similar requirement:

if (!process.env.prev_restart_delay && process.env.cron_restart) {
  console.log('skipping initial launch....');
  process.exit(0);
  return;
}

this will stop the process immediately but it still will be restarted by cron. Note you should also disable autorestart (--no-autorestart)

After the subsequent start on the script prev_restart_delay env variable will be available and the condition will be skipped and your script will run.

Not ideal, but works in my case.

alexgalkin avatar Mar 06 '24 21:03 alexgalkin

I'm going through the issues to find mentions about not auto-starting a new process, which I now created a PR for: #5780

ultimate-tester avatar Mar 08 '24 19:03 ultimate-tester

Feature landed

Update PM2 to 5.4.0

$ npm install [email protected] -g
$ pm2 update

Unitech avatar May 24 '24 12:05 Unitech