如何在特定时间自动重启并发送邮件?(时区问题)

如何在特定时间自动重启并发送邮件?(时区问题)

在 Google 上搜索,尝试在 Ubuntu 12.04 上进行一些尝试,但它的工作方式很奇怪...这些是我的 crontab 条目,

  1. 每 4 分钟启动一次控制某事物(不相关)的脚本
  2. 凌晨 3 点重启系统
  3. 凌晨 3 点发送一封主题为“系统重启”的邮件

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

    0 3 * * * 重启

    0 3 * * * echo "系统已重新启动!" | mail -s "系统已重新启动"[电子邮件保护]

现在,发送邮件操作肯定是错误的同时执行重启操作(重启时很难执行某些操作)并且我计划将其移动到“10 3”以确保万无一失。

忽略这一点,昨天我安装了 crontab 操作,今天收到了包含到达时间的电子邮件早上9点。我有点困惑。语法似乎正确...如果邮件根本没有到达,我可以理解,但为什么是上午 9 点而不是凌晨 3 点?

(我实际上无法对该机器进行大量测试,因为它是生产机器)

编辑:通过电子邮件发送原始数据

   Delivered-To: [email protected] 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: <[email protected]> Received: from mysite.it ([188.226.132.38])
   by mx.google.com with ESMTP id q19si3557509wra.220.2017.02.23.00.00.05
   for <[email protected]>;
   Thu, 23 Feb 2017 00:00:05 -0800 (PST) Received-SPF: temperror (google.com: error in processing during lookup of [email protected]: DNS error) client-ip=188.226.132.38; Authentication-Results: mx.google.com;
   spf=temperror (google.com: error in processing during lookup of [email protected]: DNS error) [email protected] 
   Received: by mysite.it (Postfix, from userid 0) id 3968F4071F; Thu, 23 Feb 2017 03:00:02 -0500 (EST) 
   Subject: System was rebooted To: <[email protected]> 
   X-Mailer: mail (GNU Mailutils 2.99.98) 
   Message-Id: <[email protected]> 
   Date: Thu, 23 Feb 2017 03:00:02
-0500 (EST) From: root <[email protected]>

   **Originale Message

   ID message   <[email protected]>
   Created: 23 feb 2017 09:00 (recapitato dopo 6 secondi)
   From:    root <[email protected]>Tramite mail (GNU Mailutils 2.99.98)
   To:  [email protected]
   Oggetto: System was rebooted**

检查电子邮件时显示两个不同的日期......

答案1

开始:

您的时区和/或当地时间错误,这就是任何作业都无法按预期运行的原因。

要设置时间和时区,请使用 Ubuntu 设置中的“时间和日期设置”小程序,或者在终端运行:

sudo timedatectl set-timezone <timezone>

或者

sudo dpkg-reconfigure tzdata

要在终端中设置时间,请使用:

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

虽然我强烈建议设置 ntp 来自动保持你的时间最新。

为了解决问题的另一部分,您可以将任务放入 crontab 文件中,并用 替换时间@reboot。这将在系统启动时运行该任务,因此您可以通过这种方式运行电子邮件脚本。

相关内容