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.
Show the cron logs that prove this.
Perhaps cron or the system runs on a different timezone?
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.
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.
/etc/timezone
contained UTC.
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.