0

I added a job for backup to my cron jobs, using crontab -e:

0 0 * * * tar -czf /somewhere/backup.tar.gz --exclude='sth/backup' --directory='/var/www' sth

But when I check the created file, with stat, it shows this:

Access: 2015-08-26 11:55:48.000667280 +0430
Modify: 2015-09-03 08:30:07.946818661 +0430
Change: 2015-09-03 08:30:07.946818661 +0430

As you see, the modification time of the file is 08:30. But in the crontab, I have set this to execute on 00:00 (12 at midnight).

Why is this happening?

Ahmad
  • 103
  • 7
  • Perhaps it took 8.5 hours to complete? – bskool Sep 03 '15 at 08:20
  • Oh, no! It takes about 4 or 5 seconds only. – Ahmad Sep 03 '15 at 08:21
  • What is the output of `crontab -u USER -l` – bskool Sep 03 '15 at 08:24
  • A lot of comments, followed by my commands, including the one that is in the question. – Ahmad Sep 03 '15 at 08:27
  • Is the correct timezone set? Also please check your hardware clock. – bskool Sep 03 '15 at 08:55
  • 1
    I'd guess that this is because of time zone confusion. The output from stat suggests that your shell is operating in UTC+0430 (Afghanistan?). Sounds like cron is running in a different time zone. Looking at /var/log/cron might clarify this. – Paul Haldane Sep 03 '15 at 09:00
  • @PaulHaldane, both of the commands are running on same server, so what do you mean exactly? This is not afghanistan, it's Iran, And I could not find /var/log/cron. I'm on a debian. – Ahmad Sep 03 '15 at 09:08
  • See http://askubuntu.com/q/54364 for possible solutions. Set a cron job to run `date>/sonewhere/test.txt` at midnight and see what time zone is in the output file. – cxw Sep 03 '15 at 09:41
  • @cxw I will try that and will let you know – Ahmad Sep 03 '15 at 10:13
  • @cxw I tried that and this is the answer: `Fri Sep 4 08:30:01 IRDT 2015` – Ahmad Sep 04 '15 at 07:55
  • Last question (I hope)- what is `ls -l /etc/localtime`? Your cron appears to be running in `America/New_York` or some other UTC-4 time zone, and I'm wondering where that setting is coming from. (Note: once we sort this out, I'm going to post an answer and delete my comments :) ) – cxw Sep 04 '15 at 09:57
  • No, I believe my local time is right. But one possibility came to my mind today. The server is up about 102 days. Maybe sometime I've changed my timezone, and did not restart cron. So I restarted and waiting to see what happens. – Ahmad Sep 04 '15 at 10:05
  • What news after the restart? – cxw Sep 07 '15 at 10:33
  • I forgot to check that! Everything is OK now! Thanks! – Ahmad Sep 07 '15 at 10:45

1 Answers1

0

The problem was originating from that I had changed the server's timezone a long time ago, but because I did not restart my server for several months, the changes did not apply to the cron service.

The main point is you have to restart the cron service after changing the timezone in order to take effect.

Ahmad
  • 103
  • 7