栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux-crond 定时任务

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux-crond 定时任务

1-crontab 命令六连-服务管理


systemctl status crond
systemctl start crond
systemctl restart crond
systemctl enable crond
systemctl disable crond.service
systemctl stop crond

-conf
ls -al /etc/crontab

2-crontab 命令参数详情


 -u  define user
 -e         edit user's crontab
 -l         list user's crontab
 -r         delete user's crontab
 -i         prompt before deleting

3-crontab格式


column 1:分钟,0~59
column 2:小时,0~23
column 3:日期,1~31
column 4:月份,1~12
column 5:星期,0~7 0和7表示星期天
column 6:要运行的命令 如果有多个命令用 && 隔开
字符介绍
星号(*) :代表所有可能的值,例如,month字段如果是星号,则表示在满足其它字段的制约条件后,每月都执行该命令操作;
逗号(,) :可以用逗号隔开的值,指定一个多元素的列表,例如:1,2,5,7,8,9
中杠(-) :可以用整数之间的中杠表示一个整数范围,例如“2-6”表示:2,3,4,5,6
正斜线(/) :可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。

4-任务执行

crontab -e 进入
20 6 * * * /usr/bin/sh /admin/shell/1.sh

问题-权限、路径

5-查看crontab的日志记录


linux
tail -f /var/log/cron
unix
/var/spool/cron/tmp
mail
/var/spool/mail/root

6-扩展 日常运维检测脚本

function modifyCrontab()
{
    num=`crontab -l | grep iostat| wc -l`
    if [[ $num -lt 1 ]];then
        echo "* * * * * iostat -xmt 2 29   >> /var/log/stat.iostat.$(date +%m%d).log" >> /var/spool/cron/root
    fi

    num=`crontab -l | grep mpstat| wc -l`
    if [[ $num -lt 1 ]];then
        echo "* * * * * mpstat -P ALL 2 29 >> /var/log/stat.mpstat.$(date +%m%d).log" >> /var/spool/cron/root
    fi

    num=`crontab -l | grep dstat | wc -l`
    if [[ $num -lt 1 ]];then
        echo "* * * * * dstat -t -c -d -n -g -y -m 1 59 >>/var/log/stat.dstat.$(date +%m%d).log" >> /var/spool/cron/root
    fi
    
    num=`crontab -l | grep find | grep rm  | grep stat | wc -l`
    if [[ $num -lt 1 ]];then
        echo "30 0 * * *  find /var/log/ -name "stat.*.log*" -mtime +3 -exec rm -f {} ;" >> /var/spool/cron/root
    fi

}
modifyCrontab

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/841536.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号