Score:0

Possible to set crontab to run at a different timezone interval?

kz flag

I have a bash script as part of my deployment process that looks something like this, and it installs different cron jobs to be run

#!/bin/bash

echo 'starting after_install.sh'

## unpack and install code ...

## write out current crontab, first configure cron to run as PST
## we want this to run as myuser, not as sudo
sudo -u myuser bash << eof
echo 'remove the current cron tasks'
crontab -r
echo 'running bundle install'
/home/myuser/.rvm/gems/ruby-2.4.1/wrappers/bundle install
echo 'install cron tasks'
(crontab -l 2>/dev/null; echo "TZ=America/Los_Angeles")| crontab -
(crontab -l 2>/dev/null; echo "CRON_TZ=America/Los_Angeles")| crontab -
(crontab -l 2>/dev/null; echo "00 00 * * * /bin/bash /var/www/application/bin/midnight.sh")| crontab -
(crontab -l 2>/dev/null; echo "00 04 * * * /bin/bash /var/www/application/bin/monday_hr_emails.sh")| crontab -
(crontab -l 2>/dev/null; echo "00 09 * * * /bin/bash /var/www/application/bin/daily-notifications.sh")| crontab -
(crontab -l 2>/dev/null; echo "30 13 * * * /bin/bash /var/www/application/bin/daily-synchronizations.sh")| crontab -
(crontab -l 2>/dev/null; echo "30 * * * * /bin/bash /var/www/application/bin/job-data.sh")| crontab -
(crontab -l 2>/dev/null; echo "00 * * * * /bin/bash /var/www/application/bin/oauth.sh")| crontab -
crontab -l
eof

## do some other stuff ...

however, the tasks stubborn and still run at UTC instead of Los Angeles - is there something I can do to fix?

Score:0
ru flag

CRON is a daemon that bases the date/time info on the system date/time, not whatever time zone that you decide to set. So if your server is running on UTC time, the cron jobs will all kick off based on the server time. I considered that maybe you could stop or kill cron, change the TZ, start cron again. But I don't believe that would work because when it spawns (or respawns), it is init.d that actually starts it if I recall. And init.d would still be using the system (UTC) time.

I am not sure what you are trying to accomplish with this script, so maybe you can clarify. Just guessing here... Is it that you want to insert jobs using the user's local time? So when someone in Boston wants to run a job at midnight and someone in Anaheim also wants a job at midnight, they don't have to figure out the offset?

I think your best approach is to convert all of your desired times into UTC time (or server time), and then just let the system handle it the way it expects to. If the problem is users/admins that are not sure about UTC time and entered a job to start at the wrong time thinking it was "local time" (been there, done that), you could in theory write a script that let's you specify the TZ while entering the CRON job. Something like:

addcron -z "America/NewYork" 00 00 * * * $HOME/midnight_job.sh

And what the addcron script would do is figure out the values for the minute and hour fields in UTC time, replacing 00 00 with 00 19 by determining that the selected TZ is UTC-5, before actually adding it to the cron table. Keeping in mind that you may also have to deal with weekdays as well! (10:30pm EST on Sundays would be 3:30am on Mondays in UTC)

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.