Date: 2021-08-04 17:20:56, Hit: 132

需求描述:

  早上设置了ntp客户端的定时任务,发现不断的有You have new mail in /var/spool/mail/root这种提示.

  然后,就看了具体的文件,由于ntpdate是每分钟执行,执行的结果,都输出到mail邮件里面了.所以,担心

  这个给撑爆了,就预先放置吧,看看如何在crontab不进行标准或错误输出.

操作过程:

1.查看定时任务,和mail信息

[[email protected] mail]# crontab -l
*/1 * * * * /usr/sbin/ntpdate 192.168.53.22
[[email protected] mail]# crontab -l   #一分钟之后,再次执行就报了mail的提示.
*/1 * * * * /usr/sbin/ntpdate 192.168.53.22
You have new mail in /var/spool/mail/root

2.查看这个mail中到底是什么

[[email protected] mail]# ls -l /var/spool/mail/root 
-rw------- 1 root mail 188542 Aug  8 10:59 /var/spool/mail/root  #最近刚有新的信息输入
You have new mail in /var/spool/mail/root
[[email protected] mail]# more /var/spool/mail/root   #查看文件的内容就是发送邮件的信息和执行命令的输出.就是发送邮件的信息,把你执行的命令结果,发送出去.
From [email protected]  Tue Aug  7 17:29:36 2018
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
Received: by testvm02.localdomain (Postfix, from userid 0)
    id A4E68440BC; Tue,  7 Aug 2018 17:29:36 +0800 (CST)
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <[email protected]> ntpdate 192.168.53.22
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <[email protected]>
Date: Tue,  7 Aug 2018 17:29:36 +0800 (CST)

/bin/sh: ntpdate: command not found

From [email protected]  Tue Aug  7 17:30:01 2018
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
Received: by testvm02.localdomain (Postfix, from userid 0)
    id A83D7440BD; Tue,  7 Aug 2018 17:30:01 +0800 (CST)
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <[email protected]> ntpdate 192.168.53.22
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF-8>

3.通过重定向将命令执行的错误和标准输出都屏蔽掉

[[email protected] mail]# crontab -e
crontab: installing new crontab
[[email protected] mail]# crontab -l
*/1 * * * * /usr/sbin/ntpdate 192.168.53.22 >/dev/null 2>&1

备注:这样执行就没有标准和错误输出了.

4.再次查看文件是否有增长


[[email protected] mail]# ls -l root
-rw------- 1 root mail 191901 Aug  8 11:03 root
[[email protected] mail]# ls -l root
-rw------- 1 root mail 191901 Aug  8 11:03 root
[[email protected] mail]# ls -l root
-rw------- 1 root mail 191901 Aug  8 11:03 root
[[email protected] mail]# date
Wed Aug  8 11:05:05 CST 2018

备注:该文件中的内容没有增加,以后也不会出现不断增加,可能会撑爆磁盘的情况了.

另:在其他的博客中设置echo "unset MAILCHECK">>/etc/profile 可以不检查mail目录中的内容,但是只是不检查发送邮件,但是root的内容还在增加.

5.彻底关闭cron进程发送邮件的方式

5.1crontab文件中,开头增加MAILTO="",表示没有收件人

[[email protected] mail]# crontab -l
MAILTO=""
*/1 * * * * /usr/sbin/ntpdate 192.168.53.22

5.2crontab定时任务增加输出重定向

*/1 * * * * /usr/sbin/ntpdate 192.168.53.22 >/dev/null 2>&1

备注:这样的话/var/spool/mail中root文件就不会在增加了,否则即使是unset MAILCHECK不提示有邮件了,但是这个文件还是在增加.

Edit Delete
License by Moony