1

I am in Singapore and the server (Ubuntu 22.04 EC2 instance) is set according to the my local timezone (UTC+8H), at least so the date command tells me.

$ date
Wed Sep 20 09:07:42 +08 2023

Is this a conclusive proof that my timezone setting is local? Unfortunately, my @daily cronjobs run everyday at 8 am local time, which suggests somehow the cron daemon is following UTC time. Is there a separate timezone setting for the cron daemon other than what I am seeing in the terminal? This is my cron command.

@daily cd tensorflow_clustering/ && bash run_index.sh >> run.log

According to the comment,

$ timedatectl
               Local time: Wed 2023-09-20 10:03:11 +08
           Universal time: Wed 2023-09-20 02:03:11 UTC
                 RTC time: Wed 2023-09-20 02:03:10
                Time zone: Asia/Singapore (+08, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
Della
  • 505
  • 1
    What happens if you replace @daily with 0 0 * * *? They are equivalent. Your problem is unusual. I have tried both of these in cron. I don't live in UTC. – user68186 Sep 20 '23 at 02:17
  • 2
    A simple test is to add * * * * * timedatectl > /tmp/date to your cron, wait a minute and then check the file /tmp/date. Does that show a different output than when you run timedatectl manually? – terdon Sep 25 '23 at 10:53

1 Answers1

2

If you want to change when the job runs:

See here for a system-wide solution that basically changes /etc/timezone which cron respects, according to man 8 cron.

If you want to keep the system timezone and only change it for cron, you can try to put TZ=Asia/Singapore in systemctl edit cron.service, and then systemctl restart cron.

See here and here for possible per-job solutions.

If you want to change the timezone the job runs with:

Cronjobs respect the TZ environment variable provided by the cron daemon. The cron daemon allows setting environment variables in crontabs.

That means you can influence the timezone of cronjobs by setting TZ in your crontabs, e. g. in crontab -e:

@daily echo "I run with UTC"

This timezone will apply to all cronjobs after this line

TZ=Asia/Singapore

@daily echo "I run with Singapore time"

Be aware that setting TZ in crontabs only affects the cronjobs, not cron itself, see above.

This defines the "local" timezone the job gets to use. It might be helpful for whatever the job does. Things like log message formats, the date command or the Python datetime.timezone object might use the "local" timezone.

riha
  • 121