1

When scheduling a job every x hours, how to tell which hours will be picked?

For example, we have a cronjob set for */8 which is run at 2AM, 10AM, etc. instead of midnight, 8AM, etc.

5 Answers 5

1

Show the cron logs that prove this.

Perhaps cron or the system runs on a different timezone?

1
  • You're right. See my answer for more details.
    – raphink
    May 25, 2012 at 11:11
2

Try this:

0 2-18/8 * * * /path/to/your/script
0

To clarify the situation, we actually saw graphs that showed that the job started at 2AM, 10AM, etc.

Upon investigating the issue, we found that cron is launched with TZ=UTC, while /etc/timezone is set to Europe/Zurich. It seems cron was restarted at a time when /etc/timezone contained UTC, and was never restarted since /etc/timezone was changed.

As a result, cron not only launches its jobs in UTC, it also logs in UTC, which results in /var/log/auth.log containing things like:

May 25 08:19:57 ip-10-50-81-194 sshd[6216]: pam_unix(sshd:session): session opened for user root by (uid=0)
May 25 06:25:01 ip-10-50-81-194 CRON[6385]: pam_unix(cron:session): session opened for user root by (uid=0)
May 25 06:29:44 ip-10-50-81-194 CRON[6385]: pam_unix(cron:session): session closed for user root
May 25 08:53:36 ip-10-50-81-194 sudo:   nagios : TTY=unknown ; PWD=/ ; USER=root ; COMMAND=/opt/nagios-plugins/check_system_update.pl

Since collectd is properly running in Europe/Zurich, it shows jobs starting at 2AM (midnight UTC), 10AM (8AM UTC), etc.

0

passing TZ variable will always make the script run in that timezone irrespective of your system local timezone. If you don't want that behavior remove the TZ variable from the line.

1
  • As said in my answer, I don't have a TZ variable on the command line. It's a problem that came from restarting cron at a point when /etc/timezone contained UTC.
    – raphink
    May 25, 2012 at 10:03
0

The * stand for firs-last so if you set up cronjob like

0 */2 * * * echo "runat midn, 2am, 4am ..., everyday"

should be the same as

0 0-23/2 * * * echo "runat midn, 2am, 4am ..., everyday"

so in your example jobs like

0 0-23/8 * * * command

or

0 */8 * * * command should run at 0:00 8:00 16:00 ...

Maybe your time or time setting are wrong. Show us your crontab -l output and check your current time and time zone settings.

1
  • See my answer for the explanation.
    – raphink
    May 25, 2012 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .