需求描述:

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

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

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

操作过程:

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

[root@testvm02 mail]# crontab -l
*/ * * * * /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------- root mail Aug : /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 ::
Return-Path: <root@testvm02.localdomain>
X-Original-To: root
Delivered-To: root@testvm02.localdomain
Received: by testvm02.localdomain (Postfix, from userid )
id A4E68440BC; Tue, Aug :: + (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-
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF->
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: <.A4E68440BC@testvm02.localdomain>
Date: Tue, Aug :: + (CST) /bin/sh: ntpdate: command not found From root@testvm02.localdomain Tue Aug ::
Return-Path: <root@testvm02.localdomain>
X-Original-To: root
Delivered-To: root@testvm02.localdomain
Received: by testvm02.localdomain (Postfix, from userid )
id A83D7440BD; Tue, Aug :: + (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-
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF->

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

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

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

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

[root@testvm02 mail]# ls -l root
-rw------- root mail Aug : root
[root@testvm02 mail]# ls -l root
-rw------- root mail Aug : root
[root@testvm02 mail]# ls -l root
-rw------- root mail Aug : root
[root@testvm02 mail]# date
Wed Aug :: CST

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

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

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

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

[root@testvm02 mail]# crontab -l
MAILTO=""
*/ * * * * /usr/sbin/ntpdate 192.168.53.22

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

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

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

文档创建时间:2018年8月8日11:10:02

linux避免crontab的执行输出将磁盘目录占满?用户的mail占用大的空间?的更多相关文章

  1. Linux中crontab无法执行java程序的问题

    Linux中crontab无法执行java程序的问题 jdk环境变量配置问题:可以用全路径来解决: 竟然是kill -9之后,后面的脚本都不运行,这个还没有找到原因啊: 即便是系统级别的 ls 命令, ...

  2. linux设置crontab定时执行脚本备份mysql

    前言:mysqldump备份数据库命令 mysqldump -u root -psztx@2018 fengliuxiaosan > /dbbackup/fengliuxiaosan.sql## ...

  3. 如何使用Linux的Crontab定时执行PHP脚本的方法

    我们的PHP程序有时候需要定时执行,我们可以使用ignore_user_abort函数或是在页面放置js让用户帮我们实现.但这两种方法都不太可靠,不稳定.我们可以借助Linux的Crontab工具来稳 ...

  4. 如何使用Linux的Crontab定时执行PHP脚本的方法[转载]

    首先说说cron,它是一个linux下的定时执行工具.根用户以外的用户可以使用 crontab 工具来配置 cron 任务.所有用户定义的 crontab 都被保存在/var/spool/cron 目 ...

  5. linux定时任务crontab怎样执行root命令

    在/var/spool/cron/文件夹下放置了所有定时任务文件, 1.运行crontab -u $用户名 -e会显示$用户的所有定时任务: 2.运行sudo crontab -e会显示root用户所 ...

  6. Linux使用crontab定时执行Python脚本清理日志

    Linux中,周期执行的任务一般由crond这个守护进程来处理.cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.crond的配置文件称为"crontab", ...

  7. linux下crontab定时执行本地脚本和定时访问指定url

    https://my.oschina.net/u/2487410/blog/683308 使用linux curl命令讲解:http://www.linuxdiyf.com/linux/2800.ht ...

  8. 使用Linux的Crontab定时执行PHP脚本

    0 */6 * * * /home/kdb/php/bin/php /home/kdb/apache/htdocs/lklkdbplatform/kdb_release/Crontab/index.p ...

  9. linux下crontab定时执行shell脚本调用oracle 存储过程

    问题:脚本内调用存储过程,脚本直接执行没问题,使用crontab 执行脚本存储过程未执行 原因:缺少oracle环境变量 解决:在shell脚本里添加oracle的环境变量 #!/bin/sh PAT ...

随机推荐

  1. win10无法访问局域网共享文件?解决如此简单。。。。。

    1 笔记本系统win10 X64企业版,其中一文件夹已设置为“共享”.本地帐号登录系统. 2 平板电脑系统win8.1 X64专业版,可以顺畅的访问笔记本的共享文件.微软帐号登录系统. 3 平板电脑系 ...

  2. 编写.info文件

    概述 drupal用.info文件来存储主题和模块的metadata. 对于模块来说,.info文件用于: 为Drupal站点管理界面解析信息: 为模块的激活和卸载提供标准和需求: 提醒drupal一 ...

  3. hbase源码系列(八)从Snapshot恢复表

    在看这一章之前,建议大家先去看一下snapshot的使用.这一章是上一章snapshot的续集,上一章了讲了怎么做snapshot的原理,这一章就怎么从snapshot恢复表. restoreSnap ...

  4. MVC教程七:扩展HtmlHelper方法

    在上一篇文章的最后,列出了一些常见的HtmlHelper的方法,这些都是ASP.NET MVC已经定义好的,如果我们想自己定义一个HtmlHelper方法可以吗?答案是肯定的,那么如何自定义一个Htm ...

  5. Altium Designer 规则设置

    设计规则设置 Designer Rules Check(DRC) Electrical 电气规则.安全间距,线网连接等 Routing 布线,线宽.过孔形状尺寸.布线拓扑.布线层.封装出线等 SMT ...

  6. 小程序的tab标签实现效果

    swiper制作tab切换 index.html ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <view class="swiper-tab&qu ...

  7. 解决java.lang.IllegalStateException: The application’s PagerAdapter changed the adapter’s content

    A界面中有viewpager的动态加载,从界面A跳到界面B,再finish掉B返回A时报出此异常. java.lang.IllegalStateException: The application's ...

  8. Linux下面makefile编写

    源程序的编译 在Linux下面,如果要编译一个C语言源程序,我们要使用GNU的gcc编译器.   下面我们以一个实例来说明如何使用gcc编译器. 假设我们有下面一个非常简单的源程序(hello.c或者 ...

  9. backup & restore On Ubuntu

    详见:https://help.ubuntu.com/community/BackupYourSystem/TAR 在 使用Ubuntu之前,相信很多人都有过使用Windows系统的经历.如果你备份过 ...

  10. Ubuntu 安装软件方法

    Ubuntu软件安装有几种方式,常用的是deb包的安装方式,deb是debian系列的Linux包管理方式,Ubuntu软件安装属于debian的派生,也默认支持这种软件安装方式,当下载到一个deb格 ...