Cron排程:hour參數是否為24h格式?


11

為了在凌晨4點夜間運行cron,我需要寫4 * * * *嗎?

要在下午4點運行,應該是16 * * * *嗎?

15

No. This would run at the 4th and 16th minute of every hour.

You're looking for:

0 4,16 * * * command

That will run at both 4am and 4pm.

Or if the two commands are different:

0 4 * * * command
0 16 * * * another_command
  1. The first column sets the minutes. In these examples, I'm setting it at 0 so the event happens on the hour.

  2. The in the next column we set the hour you want it to run. We use absolute values but you can use */2 for "every other hour", etc.

  3. The next column says that we want this to happen every day of the month.

  4. The next column denotes the months that this should trigger on (all in this case).

  5. The last column says which days of the week this is allowed to trigger on. 0-7 (where both 0 and 7 are Sunday). * means it can trigger on any day of the week.


0

No, that's not correct. The meaning of the fields

* * * * *

for each asterisk slot respectively:

  • minute (0 - 59)
  • hour (0 - 23)
  • day of month (1 - 31)
  • month (1 - 12)
  • day of week (0 - 6) (Sunday=0 or 7)

So for everyday 4am and 4pm you should use:

0 4,16 * * *

If you leave a slot just as * it will mean every iteration of that time unit i.e. every hour, daily etc.

Here, the part 0 4,16 means it will run at 0 minute of 4am and 4pm (i.e 4:00am and 4:00pm). If you wanted to run it at, let's say 15 minutes of 4am and 4pm instead you would change it to 15 4,16 * * * (i.e. 4:15am and 4:15pm).