How to reboot automatically at a certain hour and mail it happened? (timezone issue)

0

Googled it, tried and made some attempts to do it on Ubuntu 12.04, but it's working in a strange way... These are my crontab entries, that

  1. launch a certain script controlling something (not relevant) every 4 minutes
  2. at 3 am reboot the system
  3. at 3 am send an mail with topic'System rebooted'

    */4 * * * * /root/script.sh >> /root/script.log

    0 3 * * * reboot

    0 3 * * * echo "System rebooted!" | mail -s "System was rebooted" email@gmal.com

Now, surely is an error to put the send mail action after the reboot action at the same time (it's difficult to do something while rebooting) and i'm planning to move it to '10 3' just to be sure.

Ignoring this, yesterday I installed the crontab actions and today the email arrived with arrival time 9 am. I'm a bit puzzled. The syntax seems right... I would understand if the mail did not arrive at all, but why at 9am instead of 3am?

(I can't really do a lot of tests for the machine is a production one)

EDIT: Email Original Data

   Delivered-To: myemail@gmal.com Received: by 10.12.169.80 with SMTP id z16csp372030qva;
   Thu, 23 Feb 2017 00:00:11 -0800 (PST) X-Received: by 10.223.148.230 with SMTP id 93mr27510733wrr.13.1487836811929;
   Thu, 23 Feb 2017 00:00:11 -0800 (PST) Return-Path: <root@mysite.it> Received: from mysite.it ([188.226.132.38])
   by mx.google.com with ESMTP id q19si3557509wra.220.2017.02.23.00.00.05
   for <myemail@gmal.com>;
   Thu, 23 Feb 2017 00:00:05 -0800 (PST) Received-SPF: temperror (google.com: error in processing during lookup of root@mysite.it: DNS error) client-ip=188.226.132.38; Authentication-Results: mx.google.com;
   spf=temperror (google.com: error in processing during lookup of root@mysite.it: DNS error) smtp.mailfrom=root@mysite.it 
   Received: by mysite.it (Postfix, from userid 0) id 3968F4071F; Thu, 23 Feb 2017 03:00:02 -0500 (EST) 
   Subject: System was rebooted To: <myemail@gmal.com> 
   X-Mailer: mail (GNU Mailutils 2.99.98) 
   Message-Id: <20170223080002.3968F4071F@mysite.it> 
   Date: Thu, 23 Feb 2017 03:00:02
-0500 (EST) From: root <root@mysite.it>

   **Originale Message

   ID message   <20170223080002.3968F4071F@mysite.it>
   Created: 23 feb 2017 09:00 (recapitato dopo 6 secondi)
   From:    root <root@mysite.it>Tramite mail (GNU Mailutils 2.99.98)
   To:  myemail@gmal.com
   Oggetto: System was rebooted**

Inspecting the email two different dates are showing...

Sasha Grievus

Posted 2017-02-23T09:17:29.327

Reputation: 103

1Have you inspected Received: headers of the email message? Could you publish them? – AnFi – 2017-02-23T11:04:07.490

done mysite.it for my site and myemail@gmal.com for my email – Sasha Grievus – 2017-02-23T11:26:30.217

1You should look at the cron nickname @reboot, which will kick off a job after a reboot occures. – charlesbridge – 2017-02-23T13:31:12.580

1I guess the time discrepancy is down to your time zone not being set properly. What time zone are you in, what does the local time say and what is the output of 'date' and 'date -u'? – Darren – 2017-02-23T13:35:49.893

good point! Actually 'date' returned "Thu Feb 23 09:20:58 EST 2017 " at 15:20 p.m. in my local time! I'll change it. Someone wants to compile a proper answer (it would be great if the method to align time zone is in and the @reboot thing, too) just for people passing by – Sasha Grievus – 2017-02-23T14:52:00.047

Answers

1

Here goes:

Your time zone and/or local time is wrong which is why any jobs are not running as expected.

To set the time and timezone, use the "time and date settings" applet in Ubuntu settings, or in a terminal run:

sudo timedatectl set-timezone <timezone>

or

sudo dpkg-reconfigure tzdata

To set the time in a terminal use:

sudo date -s "yyyy-mm-dd hh:mm:ss"

Although I would strongly suggest setting up ntp to keep your time current automatically.

To address the other part of your question, you can put a task in a crontab file and substitute the time with @reboot. This will run the task on system startup so you can run your email script that way.

Darren

Posted 2017-02-23T09:17:29.327

Reputation: 2 435