Issue
I have a crontab scheduling many things on an Ubuntu server which runs on UTC time. Some of the scripts being run are time sensitive in relation to the web applications they are providing functionality for.
e.g. Something has to be updated at 4pm every day, as the end users see it by their clock.
This is not an issue during the winter when the UK is on GMT, but from April to October, the clocks go forward an hour for British Summer Time (BST). The scripts running on the server then update the web application at what appears to be 5pm for the users, instead of 4pm.
Is there a way to conditionally adjust the crontab's scheduled times for this time zone change?
The intention below is to run on the first day of every month at 00:00. During BST, my understanding is that this will run at 01:00 BST during daylight savings:
# m h dom mon dow user command
0 0 1 * * root cd / && run-parts --report /etc/cron.monthly
What options do I have to ensure this always runs at 00:00 during both GMT and BST?
Server Timezone Info
$ timedatectl
Local time: Mon 2021-11-01 22:57:41 UTC
Universal time: Mon 2021-11-01 22:57:41 UTC
RTC time: Mon 2021-11-01 22:57:41
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
NTP service: n/a
RTC in local TZ: no
Solution
In my comments above, I praised Cyrus's reply, which I thought sounded very promising. Apparently, he disagreed, and deleted his response. Oh well...
This might be a good alternative for you:
https://www.the-art-of-web.com/system/cron-set-timezone/
If you are in a different time zone to some of your clients, or have a server that uses UTC to avoid daylight saving changes, it can be tricky to set a CRON script running at the correct time in a different time zone. Here we explore a couple of solutions. ...
[You can] specify a time zone for CRON:
# m h dom mon dow command TZ=Australia/Sydney 0 0 * * * www-data /usr/bin/php $CRONSCRIPT
Unfortunately, setting the time zone here has no effect on when the CRON script is triggered. What it does instead is set an environment variable inside the shell in which the script is called. The script itself may then use the TZ time zone...
# m h dom mon dow command SHELL=/bin/bash TZ=Australia/Sydney 0 * * * * www-data [ "$(date +\%H\%M)" == "0000" ] && /usr/bin/php $CRONSCRIPT
Success! We now have a means of triggering a script at midnight in any time zone. The CRON command will be triggered every hour, but only continue to running the PHP script if it's midnight in the target time zone.
Just substitute your task for "php", and you should be good to go!
Answered By - paulsm4 Answer Checked By - David Marino (WPSolving Volunteer)