timezone aware cron jobs (adjust for DST) - cron

I have a need to download files from all over the world to the same server. The files become available at the same time each day in the local timezone. The challenge is daylight savings time. Because different countries adjust on different days, and some don't do DST at all, the offset between any 2 timezones changes throughout the year. So it is not possible to convert everything into a single timezone. Is there a way to handle this with cron? Or something similar to cron?
I already tried this and it does not do what I want:
TZ=Asia/Tokyo
05 15 * * * echo $(date) JAPAN >> /tmp/crontest1.txt 2>&1
TZ=Europe/London
05 15 * * * echo $(date) LONDON >> /tmp/crontest2.txt 2>&1
TZ=America/New_York
05 15 * * * echo $(date) NY >> /tmp/crontest3.txt 2>&1
TZ=America/Denver
05 15 * * * echo $(date) DENVER >> /tmp/crontest4.txt 2>&1
everything runs at 15:05 in the timezone of the server and the output is
Fri Mar 24 04:05:01 JST 2017 JAPAN
Thu Mar 23 19:05:01 GMT 2017 LONDON
Thu Mar 23 15:05:01 EDT 2017 NY
Thu Mar 23 13:05:01 MDT 2017 DENVER
so while each process runs with the TZ value set correctly it runs at the time of the main server. I need something that adjusts the start time for DST.

Related

Crontab bad day-of-week when trying to run 3rd wednesday of the month

Have tried a schedule of below to get a job to run at 7:30 on the 3rd wednesday of the month
30 07 * * wed#3
30 07 * * 4#3
Get below error
"/tmp/crontab.kE5RJg":76: bad day-of-week
errors in crontab file, can't install
Curretly using below
30 07 15-21 * wed

Crontab File Empty But Jobs Running

I'm having a hard time understanding some cron behavior that I was wondering if someone might be able to shed some light on.
I have a server running Centos 6.6. If I run "sudo cat /etc/crontab", the following output is displayed:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR
sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
As you can see, there are no actual jobs configured in the system crontab file. However, jobs are indeed getting run from the /etc/cron.d/, /etc/cron.daily/, and /etc/cron.hourly/ directories as you can see from the /var/log/cron file:
Jul 29 03:38:01 <servername> run-parts(/etc/cron.daily)[26524]: finished logrotate
Jul 29 03:38:01 <servername> run-parts(/etc/cron.daily)[26494]: starting update_phishing_sites
Jul 29 03:42:04 <servername> run-parts(/etc/cron.daily)[26565]: finished update_phishing_sites
Jul 29 03:42:04 <servername> run-parts(/etc/cron.daily)[26494]: starting update_spamassassin
Jul 29 03:45:02 <servername> run-parts(/etc/cron.daily)[26587]: finished update_spamassassin
Jul 29 03:45:02 <servername> anacron[26044]: Job `cron.daily' terminated (mailing output)
Jul 29 03:45:02 <servername> anacron[26044]: Normal exit (1 job run)
Jul 29 04:01:01 <servername> CROND[26719]: (root) CMD (run-parts /etc/cron.hourly)
Jul 29 04:01:01 <servername> run-parts(/etc/cron.hourly)[26719]: starting 0anacron
Jul 29 04:01:01 <servername> run-parts(/etc/cron.hourly)[26728]: finished 0anacron
Jul 29 04:01:01 <servername> run-parts(/etc/cron.hourly)[26719]: starting check_MailScanner
Jul 29 04:01:02 <servername> run-parts(/etc/cron.hourly)[26752]: finished check_MailScanner
Jul 29 04:01:02 <servername> run-parts(/etc/cron.hourly)[26719]: starting processing_messages_alert
Jul 29 04:01:02 <servername> run-parts(/etc/cron.hourly)[26766]: finished processing_messages_alert
Jul 29 04:01:02 <servername> run-parts(/etc/cron.hourly)[26719]: starting update_bad_phishing_sites
Jul 29 04:08:37 <servername> run-parts(/etc/cron.hourly)[26863]: finished update_bad_phishing_sites
Jul 29 04:08:37 <servername> run-parts(/etc/cron.hourly)[26719]: starting update_virus_scanners
Jul 29 04:14:52 <servername> run-parts(/etc/cron.hourly)[27187]: finished update_virus_scanners
My question is, what/where/how are those jobs being kicked off? I've seen examples online where the /etc/crontab file looks something like this:
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
If my /etc/crontab file contained these entries then I would understand how the jobs are getting kicked off. However as stated above, my /etc/crontab file does not contain these entries.
Chris
I should have just spent a few more minutes looking into things before posting the question because I found out how the jobs were being kicked off. The man page for crond on my server states the following:
/etc/crontab
system crontab. Nowadays the file is empty by default. Originally it was usually used to run daily, weekly, monthly jobs. By default these jobs
are now run through anacron which reads /etc/anacrontab configuration file. See anacrontab(5) for more details.
If I look at the /etc/anacrontab file, it contains the following:
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
#monthly 45 cron.monthly nice run-parts /etc/cron.monthly
So, that's how they are being kicked off. :)

Crontab executes at the wrong time

I have a crontab settings as follows:
sudo crontab -l -u bheng
Contents:
#field allowed values
# ----- --------------
# minute 0-59
# hour 0-23
# day of month 1-31
# month 1-12 (or names, see below)
# day of week 0-7 (0 or 7 is Sun, or use names)
#
# m h dom mon dow command
MAILTO="bheng#outlook.com"
#Daily
01 22 * * * php /home/mysite.com/artisan products:exportdiff --interval="yesterday"
16 22 * * * php /home/mysite.com/artisan images:exportdiff --interval="yesterday"
31 22 * * * php /home/mysite.com/artisan publications:exportdiff --interval="yesterday"
#Weekly
1 23 * * 7 php /home/mysite.com/artisan publications:exportdiff --interval="last sunday"
16 23 * * 7 php /home/mysite.com/artisan images:exportdiff --interval="last sunday"
31 23 * * 7 php /home/mysite.com/artisan products:exportdiff --interval="last sunday"
As you can see, it suppose to be kicking at 10 and 11 PM at night.
But instead, I got 3 emails at 5 PM yesterday at 5:01 PM, 5:16 PM, 5:31 PM.
I thought it was the time wrong the in system or VM so I checked it I saw UTC time.
Then, I update it by running sudo dpkg-reconfigure tzdata and set it to US Eastern time.
Now, when I ran date command I got Ex.Thu Dec 15 07:56:27 EST 2016 correctly as US EST time.
Is there some service that I need to restart?
Or is this something other crontab settings that might have overwrite my current settings ?
I believe you have to restart cron after making time / time zone related changes.
Depending on your version of cron, you might be able to restart it with sudo service cron restart.

crontab hour with UTC working hours GMT-8

i have a machine set in GMT.
i would like to have a cron task scheduled from 6AM to 5PM in PST, which is GMT-8.
gmt time zone. typical.
00 06-17 * * *
now -8 produces a crontab bad hour error.
00 22-09 * * *
the following is accepted or should i say, no error.
00 22,23,0,1,2,3,4,5,6,7,8,9 * * *
any short form therefore?
For vixie cron,
Lists and ranges are allowed to co-exist in the same field. "1-3,7-9" would be rejected by AT&T or BSD cron -- they want to see "1-3" or "7,8,9" ONLY
That is, the following:
00 22-23,0-9 * * *
You can try by changing the time zone for cron also:
suku#ubuntu-vm:~$ crontab -l | tail -2
TZ=Africa/Tripoli
* * * * * date > date.txt
suku#ubuntu-vm:~$ cat date.txt
Thu Jan 10 18:50:01 EET 2013
suku#ubuntu-vm:~$ date
Thu Jan 10 22:20:19 IST 2013
A good place to try out the basics is the cron simulator at www.dataphyx.com 1

Real-time display of `date` changes on Linux

On an embedded Linux device that does not present /dev/rtc*, how can I set off a console window writing the value of the Real-Time Clock to the console, on the tick, every time it changes?
Results would be like:
$ **someCmd**
Mon Mar 14 16:43:22 UTC 2011
Mon Mar 14 16:43:23 UTC 2011
Mon Mar 14 16:43:24 UTC 2011
Mon Mar 14 16:43:25 UTC 2011
Mon Mar 14 16:43:26 UTC 2011
etc.
The device is armv5tejl running BusyBox v1.13.3.
Use the watch commad, try this is:
watch -n 1 date
I don't know how much the BusyBox shell supports, but in sh you could do something like this:
{ while true ; do date ; sleep 0.1 ; done } | uniq

Resources