0

The following root Crontab job is being used to save periodic timestamped copies of a web page every 15 minutes. Ubuntu 16.04.2 LTS AMD64 cloud server online 24/7. Time as displayed by the date function is UTC.

*/15 * * * * /usr/bin/wget --convert-links --output-document=/home/user/Firefox/News-Headlines-$(date +\%Y-\%m-\%d_\%H:\%M:\%S).html www.news.tld

The following file is being saved as expected:

News-Headlines-2017-04-11_02:15:01.html < Time in UTC

The desired timestamp is for TZ=America/Detroit, currently -4 UTC

News-Headlines-2017-04-10_22:15:01.html < Time America/Detroit

QUESTION

How can the date command be changed to produce the filename with the TZ=America/Detroit timestamp?

News-Headlines-2017-04-10_22:15:01.html

NOTES

I obtained the proper name of the timezone from Timezone List

en.wikipedia.org/wiki/List_of_tz_database_time_zones

I am aware of these hybrid command lines to get a time shifted date from the "date" command.

Vanila date command output

$ date
Mon Apr 10 20:52:29 UTC 2017

Time shifted output

$ TZ=America/Detroit date
Mon Apr 10 16:25:49 EDT 2017

I attempted

*/15 * * * * /usr/bin/wget --convert-links --output-document=/home/user/Firefox/News-Headlines-$(TZ=America/Detroit date +\%Y-\%m-\%d_\%H:\%M:\%S).html www.news.tld

The timestamp saved is unaffected:

News-Headlines-2017-04-11_02:15:01.html  < Time in UTC
  • 1
    Honestly I don't see anything wrong with the way you are doing it - and testing with a simple * * * * * touch /tmp/crondate-$(America/Detroit date +\%Y-\%m-\%d_\%H:\%M:\%S).html appears to work for me. – steeldriver Apr 11 '17 at 1:40
  • And I don't know when you are, but in time in UTC is not yet 02:15 on 2017/04/11. (Timestamp on this comment: 2017-04-11 01:42:53Z) – muru Apr 11 '17 at 1:42
  • steeldriver: Removing TZ from my proposed code line: */15 * * * * /usr/bin/wget --convert-links --output-document=/home/user/Firefox/News-Headlines-$(America/Detroit date +\%Y-\%m-\%d_\%H:\%M:\%S).html www.news.tld . . . results in no file being saved. – ml48603 Apr 11 '17 at 1:58
  • You should really not run wget, or in general anything that processes untrusted data, as root. – fkraiem Apr 11 '17 at 19:43
  • Will setup new user for these periodic data harvests. Thanks – ml48603 Apr 13 '17 at 16:30
0

Did some experimenting with the date command from within the root crontab. From what I observed the date command run from crontab does not recognize all of the Timezone designations listed at:

en.wikipedia.org/wiki/List_of_tz_database_time_zones

Rewriting the cited command in the original post with a UTC based timezone designator as opposed to the Country/City format designator.

*/15 * * * * /usr/bin/wget --convert-links --output-document=/home/user/Firefox/News-Headlines-$(TZ=UTC4 date +\%Y-\%m-\%d_\%H:\%M:\%S).html www.news.tld

NOTE: Whether GMT or UTC is used the results are the same.

Results in the following timestamped file name timestamped for -4 UTC, despite use of the timezone designator UTC4 = UTC +4.

# date
Tue Apr 11 18:34:36 UTC 2017

News-Headlines-2017-04-11_14:34:36.html

While this solution does work, it is not understood why using timezone designator UTC4 = UTC +4 results in a UTC -4 timestamp.

Experiment

Using the UTC -4 designator results in the opposite:

$ wget --convert-links --output-document=/home/mark/Downloads/Firefox/FoxNews-Headlines-$(TZ=UTC-4 date +\%Y-\%m-\%d_\%H:\%M:\%S).html www.foxnews.com

# date
Tue Apr 11 18:50:08 UTC 2017

News-Headlines-2017-04-11_22:50:08.html

This timestamp is 4 hours ahead of UTC with a UTC-4 = UTC -4 . It is not understood why using timezone designator UTC-4 = UTC -4 results in a UTC +4 timestamp.

0

Setting the TZ environment variable globally in the crontab file works:

firas@momiji ~ % cat /etc/cron.d/timestamp 
TZ=America/Detroit

* * * * * firas /usr/bin/wget --quiet --convert-links --output-document=/home/firas/News-Headlines-$(date +\%Y-\%m-\%d_\%H:\%M:\%S).html www.fkraiem.org
firas@momiji ~ % date
Wed Apr 12 04:49:20 JST 2017
firas@momiji ~ % TZ=America/Detroit date
Tue Apr 11 15:49:34 EDT 2017
firas@momiji ~ % ls News-Headlines-2017-04-11_15:4*
News-Headlines-2017-04-11_15:46:01.html  News-Headlines-2017-04-11_15:48:01.html
News-Headlines-2017-04-11_15:47:01.html  News-Headlines-2017-04-11_15:49:01.html

Things to note:

  • Run wget as yourself and not as root, as it is in general dangerous to run anything that processed untrusted data as root.
  • Run wget with --quiet to suppress all output (which is otherwise sent by e-mail to the user).
  • Been using >/dev/null 2>&1. Will try --quiet today to test. Will setup new user for these periodic data harvests. Thanks – ml48603 Apr 13 '17 at 16:29
  • --quiet switch worked as expected and will restore error notification that was disabled with previous crontab redirect >/dev/null 2>&1 – ml48603 Apr 13 '17 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.