1. crontab 相关的命令:
安装:apt-get install cron
启动:service cron start
重启:service cron restart
停止:service cron stop
检查状态:service cron status
查询cron可用的命令:service cron
检查Crontab工具是否安装:crontab -l
2. crontab 编辑要执行的命令:
crontab -e //编辑定时任务
crontab -l //查看定时任务
输入这行命令之后,会自动打开一个文本文件,在文件文件中添加任务内容,按Ctrl + O保存文件,然后按 Ctrl + X 退出文件:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/10 * * * * /opt/jmeter/apache-jmeter-5.5/task/script.sh
上述命令是每隔10分钟执行一次脚本,脚本内容如下:
#!/bin/bash
source /etc/profile
count=`ps -ef | grep "MQTT" | grep -v grep | wc -l`
echo $count
if [ $count -eq 0 ];then
nohup /opt/jmeter/apache-jmeter-5.5/bin/jmeter -n -t /opt/jmeter/apache-jmeter-5.5/script/MQTT_Pub_Sampler.jmx -l MQTT_PUB.log &
else
kill -9 $(ps -ef |grep "MQTT" |grep -v "grep" |awk '{print $2}')
nohup /opt/jmeter/apache-jmeter-5.5/bin/jmeter -n -t /opt/jmeter/apache-jmeter-5.5/script/MQTT_Pub_Sampler.jmx -l MQTT_PUB.log &
fi
其他的一些常用的crontab命令:
crontab每分钟定时执行:*/1 * * * * service mysqld restart //每隔1分钟执行一次
*/10 * * * * service mysqld restart //每隔10分钟执行一次
crontab每小时定时执行:
0 */1 * * * service mysqld restart //每1小时执行一次
0 */2 * * * service mysqld restart //每2小时执行一次
crontab每天定时执行:
0 10 * * * service mysqld restart //每天10点执行
30 19 * * * service mysqld restart //每天19点30分执行
crontab每周定时执行:
0 10 * * 1 service mysqld restart //每周一10点执行
30 17 * * 5 service mysqld restart //每周五17点30分执行
crontab每年定时执行:
0 10 1 10 * service mysqld restart //每年的10月1日10点执行
0 20 8 8 * service mysqld restart //每年的8月8日20点执行