当前位置:   article > 正文

Ubuntu crontab定时任务

ubuntu crontab

1. crontab 相关的命令:

  1. 安装:apt-get install cron
  2. 启动:service cron start
  3. 重启:service cron restart
  4. 停止:service cron stop
  5. 检查状态:service cron status
  6. 查询cron可用的命令:service cron
  7. 检查Crontab工具是否安装:crontab -l

2. crontab 编辑要执行的命令:

  1. crontab -e //编辑定时任务
  2. crontab -l //查看定时任务

        输入这行命令之后,会自动打开一个文本文件,在文件文件中添加任务内容,按Ctrl + O保存文件,然后按 Ctrl + X 退出文件:   

  1. # Edit this file to introduce tasks to be run by cron.
  2. #
  3. # Each task to run has to be defined through a single line
  4. # indicating with different fields when the task will be run
  5. # and what command to run for the task
  6. #
  7. # To define the time you can provide concrete values for
  8. # minute (m), hour (h), day of month (dom), month (mon),
  9. # and day of week (dow) or use '*' in these fields (for 'any').
  10. #
  11. # Notice that tasks will be started based on the cron's system
  12. # daemon's notion of time and timezones.
  13. #
  14. # Output of the crontab jobs (including errors) is sent through
  15. # email to the user the crontab file belongs to (unless redirected).
  16. #
  17. # For example, you can run a backup of all your user accounts
  18. # at 5 a.m every week with:
  19. # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
  20. #
  21. # For more information see the manual pages of crontab(5) and cron(8)
  22. #
  23. # m h dom mon dow command
  24. */10 * * * * /opt/jmeter/apache-jmeter-5.5/task/script.sh

         上述命令是每隔10分钟执行一次脚本,脚本内容如下:

  1. #!/bin/bash
  2. source /etc/profile
  3. count=`ps -ef | grep "MQTT" | grep -v grep | wc -l`
  4. echo $count
  5. if [ $count -eq 0 ];then
  6. 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 &
  7. else
  8. kill -9 $(ps -ef |grep "MQTT" |grep -v "grep" |awk '{print $2}')
  9. 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 &
  10. 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点执行

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/95841
推荐阅读
相关标签
  

闽ICP备14008679号