需求描述:
早上設置了ntp客戶端的定時任務,發現不斷的有You have new mail in /var/spool/mail/root這種提示.
然后,就看了具體的文件,由於ntpdate是每分鍾執行,執行的結果,都輸出到mail郵件里面了.所以,擔心
這個給撐爆了,就預先放置吧,看看如何在crontab不進行標准或錯誤輸出.
操作過程:
1.查看定時任務,和mail信息
[root@testvm02 mail]# crontab -l */1 * * * * /usr/sbin/ntpdate 192.168.53.22
[root@testvm02 mail]# crontab -l #一分鍾之后,再次執行就報了mail的提示.
*/1 * * * * /usr/sbin/ntpdate 192.168.53.22
You have new mail in /var/spool/mail/root
2.查看這個mail中到底是什么
[root@testvm02 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 [root@testvm02 mail]# more /var/spool/mail/root #查看文件的內容就是發送郵件的信息和執行命令的輸出.就是發送郵件的信息,把你執行的命令結果,發送出去. From root@testvm02.localdomain Tue Aug 7 17:29:36 2018 Return-Path: <root@testvm02.localdomain> X-Original-To: root Delivered-To: root@testvm02.localdomain Received: by testvm02.localdomain (Postfix, from userid 0) id A4E68440BC; Tue, 7 Aug 2018 17:29:36 +0800 (CST) From: root@testvm02.localdomain (Cron Daemon) To: root@testvm02.localdomain Subject: Cron <root@testvm02> 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: <20180807092936.A4E68440BC@testvm02.localdomain> Date: Tue, 7 Aug 2018 17:29:36 +0800 (CST) /bin/sh: ntpdate: command not found From root@testvm02.localdomain Tue Aug 7 17:30:01 2018 Return-Path: <root@testvm02.localdomain> X-Original-To: root Delivered-To: root@testvm02.localdomain Received: by testvm02.localdomain (Postfix, from userid 0) id A83D7440BD; Tue, 7 Aug 2018 17:30:01 +0800 (CST) From: root@testvm02.localdomain (Cron Daemon) To: root@testvm02.localdomain Subject: Cron <root@testvm02> 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.通過重定向將命令執行的錯誤和標准輸出都屏蔽掉
[root@testvm02 mail]# crontab -e crontab: installing new crontab [root@testvm02 mail]# crontab -l */1 * * * * /usr/sbin/ntpdate 192.168.53.22 >/dev/null 2>&1
備注:這樣執行就沒有標准和錯誤輸出了.
4.再次查看文件是否有增長
[root@testvm02 mail]# ls -l root -rw------- 1 root mail 191901 Aug 8 11:03 root [root@testvm02 mail]# ls -l root -rw------- 1 root mail 191901 Aug 8 11:03 root [root@testvm02 mail]# ls -l root -rw------- 1 root mail 191901 Aug 8 11:03 root [root@testvm02 mail]# date Wed Aug 8 11:05:05 CST 2018
備注:該文件中的內容沒有增加,以后也不會出現不斷增加,可能會撐爆磁盤的情況了.
另:在其他的博客中設置echo "unset MAILCHECK">>/etc/profile 可以不檢查mail目錄中的內容,但是只是不檢查發送郵件,但是root的內容還在增加.
5.徹底關閉cron進程發送郵件的方式
5.1crontab文件中,開頭增加MAILTO="",表示沒有收件人
[root@testvm02 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不提示有郵件了,但是這個文件還是在增加.
文檔創建時間:2018年8月8日11:10:02