linux crond定时任务

1.crond服务管理

crond服务启动:service
crond restart( systemctl restart crond)

crond服务查看:ps aux|grep crond

设置开机自启动:chkconfig crond onsystemctl enable crond.service

查看开机自启动状态:chkconfig --list|grep crond(systemctl list-unit-files|grep crond)

2.crontab命令使用

crontab [选项]

选项:-e编辑定时任务

-l显示定时任务

-r删除定时任务

定时任务,命令格式

* * * * * 执行的任务(命令或shell脚本)

注:

1) 第一个*,表示一个小时中第几分钟,取值0-59

第二个*,表示一天中第几个小时,取值0-23

第三个*,表示一个月中第几天,取值1-31

第四个*,表示一年中第几个月,取值1-12

第五个*,表示一周中星期几,取值1-7

 

2表示不连续的时间,例如1,2,4,8 * * * * echo m>>1.txt

-表示连续的时间,例如 1-3 * * * * echo m>>1.txt

*/n表示每隔多久执行,例如*/5 * * * 1 echo m>>1.txt

 

思考:0 0 1,15 * 1 执行的任务 含义?

 

3.举例

 

3.1 定时执行命令

*/3 * * * * /root/wrk/wrk -t2000 -c2000
-d90s -v "https://XXXX.com/XXX"

3.2 定时执行脚本

*/3 * * * * /root/sh/sni.sh

#!/bin/bash

i=0

while [ $i -le 500 ]

do

curl -v https://huana1.test.cdn.aliyunlive.com/conn
-k |grep connection_id>>/root/sh/hn1.txt

echo $i huana1.test.cdn.aliyunlive.com
>>/root/sh/hn1.txt

echo $i $(date) >>/root/sh/hn1.txt

i=$(( $i+1))

done 
注: 
1)chmod修改脚本可执行权限; 2)文件路径建议用绝对路径;
 
原文地址:https://www.cnblogs.com/shadow-yin/p/14842939.html