4

Is it possible to have multiple timezones in the /etc/crontab file, like below?

CRON_TZ=Europe/Paris
*/20  *  *  *  *  root /home/user/script.sh >/dev/null 2>&1
*/5  *  *  *  *  root /home/user/script1.sh >/dev/null 2>&1

And then:

CRON_TZ=UTC
*/5  *  *  *  *  root /home/user/watchdog.sh >/dev/null 2>&1
*/10  *  *  *  *  root /home/user/watchdog1.sh >/dev/null 2>&1

I've also tried TZ=Europe/Paris and export TZ=Europe/Paris instead of CRON_TZ before each line. But it didn't work like that.


I don't want to change the original timezone of the server which is UTC. Just want to run certain tasks in different timezones.


How can I use multiple timezones like this?

MTG
  • 193
  • 6
Nikk
  • 227
  • 1
  • 4
  • 10
  • crond tunes itself by one time zone and calculates specified periods accordingly. you better calculate time shift by one central/specific tz once and add/subtract from all your entries. – MTG Apr 13 '18 at 08:01
  • 1
    @MTG That doesn't account for daylight savings! – Nikk Apr 13 '18 at 08:02
  • 1
    If your jobs run every hour, then CRON_TZ and daylight saving time are irrelevant. The jobs would run every hour regardless of its setting. Do you want to run the jobs less than hourly, based on a specified time zone, or do you want to run the jobs every hour, with a specific timezone? – Michael Hampton Apr 13 '18 at 16:34
  • @MichaelHampton The example above is to illustrate how I want it to function. While I have tasks that run hourly, I also have scheduled ones. Like backups etc that rely on a timezone. – Nikk Apr 13 '18 at 17:10

2 Answers2

1

I don't think you can put both timezones in /etc/crontab, but you can make different files in /etc/cron.d each of which has a different CRON_TZ and TZ setting.

For instance, /etc/cron.d/chicago:

CRON_TZ=America/Chicago
25 12 * * * root echo Chicago >> /tmp/TZ

And /etc/cron.d/new_york:

CRON_TZ=America/New_York
25 13 * * * root echo New_York >> /tmp/TZ

At 17:25 UTC, the content of /tmp/TZ is:

Chicago
New_York
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
0

On a CentOS 7 machine with cronie-1.4.11-14.el7_2.1.x86_64, I can have two CRON_TZ in one file. My crontab file reads:

CRON_TZ=Etc/GMT+0
 * */2 * *  * touch ~/temp/cron-0gmt0evenhour
 * 1-23/2 * *  * touch ~/temp/cron-0gmt1oddhour
CRON_TZ=Etc/GMT+1
 * */2 * *  * touch ~/temp/cron-1gmt0evenhour
 * 1-23/2 * *  * touch ~/temp/cron-1gmt1oddhour

1-23/2 means odd hours. See stackoverflow and serverfault.

Result (after it runs for long enough):

$ ll ~/temp/ --full-time
total 0
-rw-r--r-- 1 packard domain_users 0 2020-11-21 22:59:01.108221582 +0000 cron-0gmt0evenhour
-rw-r--r-- 1 packard domain_users 0 2020-11-21 23:25:01.177041834 +0000 cron-0gmt1oddhour
-rw-r--r-- 1 packard domain_users 0 2020-11-21 23:25:01.176041830 +0000 cron-1gmt0evenhour
-rw-r--r-- 1 packard domain_users 0 2020-11-21 22:59:01.108221582 +0000 cron-1gmt1oddhour

CRON_TZ only controls the timing to start a job, so it is useful only if the job does NOT run on every hour (as Michael pointed out in a comment). Use TZ to set the environment variable that the job sees.

Michael's answer (multiple cron files) would not be feasible for non-root users, who have only one crontab. And having multiple CRON_TZ is useful to deal with different daylight-saving time in different time-zones.

ps. I got to know my version of cron from the following command inspired by this post.

$ rpm -qf `which crontab`
cronie-1.4.11-14.el7_2.1.x86_64
Packard CPW
  • 101
  • 1
  • [A stackoverflow post](https://stackoverflow.com/q/47618815/9382524), also using cronie, also manage to change `CRON_TZ` in the middle of a cron file. – Packard CPW Nov 22 '20 at 01:37