Linux启动定时任务

1.首先安装cron服务
sudo apt-get install cron
2.启动与关闭cron服务
service cron start //启动服务
service cron stop //关闭服务
service cron restart //重启服务
service cron reload //重新载入配置
service cron status //查看crontab服务状态
3.编写启动服务脚本(以jmeter服务启动为列子)
vim jmeter-start.sh

#!/bin/bash
source /etc/profile
jmeter -n -t /jmeter/apache-jmeter-5.1.1/testcases/cases/jinritoutiaoguanzhu.jmx

4.定时启动服务,用到任务调度的crond常驻命令
crontab -e
在控制台写入以下内容:

0 17 * * * /jmeter-start.sh

然后保存(ctrl+x,再按y)
注意:chmod 777 jmeter-start.sh 和 jinritoutiaoguanzhu.jmx
5.定时关闭jmeter服务,用到任务调度的crond常驻命令
crontab -e

50 22 * * * ps -ef|grep jmeter|sed -n 2p|awk '{print $2}'|xargs kill -9

参数解释:
sed -n 2p ##表示截取第二行数据
awk ‘{print $2}’ ##表示截取第二列数据
xargs ##表示作为参数供kill命令使用
6.定时任务例子:

(1)表示每晚的21:30重启apache。
        30 21 * * * /usr/local/etc/rc.d/lighttpd restart
(2)表示每月1、10、22日的4 : 45重启apache。
        45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
(3)表示每周六、周日的1 : 10重启apache。
        10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
(4)表示在每天18 : 00至23 : 00之间每隔30分钟重启apache。
        0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
(5)表示每星期六的11 : 00 pm重启apache。
        0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
(6)每一小时重启apache
        * */1 * * * /usr/local/etc/rc.d/lighttpd restart
(7)晚上11点到早上7点之间,每隔一小时重启apache
        * 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
(8)每月的4号与每周一到周三的11点重启apache
        0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
(9)一月一号的4点重启apache
        0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
(10)每半小时同步一下时间
        */30 * * * * /usr/sbin/ntpdate 210.72.145.44
(11)每天18 : 00至23 : 00之间每隔30分钟重启smb 
      0,30 18-23 * * * /etc/init.d/smb restart

7.查看调度任务
crontab -l //列出当前的所有调度任务
crontab -l -u test //列出用户test的所有调度任务
8.删除任务调度工作
crontab -r //删除所有任务调度工作
9.任务执行如果报错:
CRON[9435]: (CRON) info (No MTA installed, discarding output)
解决方法:apt-get install postfix
安装时一直按enter键

发布了91 篇原创文章 · 获赞 15 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_38776582/article/details/101360988
今日推荐