当前位置: 首页 > news >正文

Ubuntu crontab定时任务

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 ];thennohup /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 &
elsekill -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点执行

http://www.lryc.cn/news/89554.html

相关文章:

  • ChatGPT Prompt Engineering for Developers 大语言模型引导词指导手册
  • 【Vue】二:Vue核心处理---模板语法
  • windows环境下nginx+ftp服务器搭建简易文件服务器
  • 【数据结构与算法】图的概述(内含源码)
  • SAP MM 根据采购订单反查采购申请
  • C语言程序设计题/C语言计算机二级考前押题版
  • Canonical标签在SEO中重要作用
  • 【Linux之进程间通信】06.Linux进程通信 - 共享内存
  • oracle安装
  • CSS样式的三种引入方式及优先级
  • Linux第二天
  • 微服务和领域驱动
  • Redis如何做到内存高效利用?过期key删除术解析!
  • EFDC模型教程
  • URLConnection(三)
  • 针对KF状态估计的电力系统虚假数据注入攻击研究(Matlab代码实现)
  • 2023-05-25 LeetCode每日一题(差值数组不同的字符串)
  • MI小米验厂知识点
  • 损失函数——交叉熵损失(Cross-entropy loss)
  • 电商ERP接口erp进销存接口
  • leetcode 922. 按奇偶排序数组 II
  • Unity四叉树地图
  • 【unity插件】OpenFracture插件实现物体破裂和切割
  • Spring Security实现登录
  • 小狐狸ChatGPT付费创作系统1.9.7独立版 + H5端 + 小程序前端增加AI绘画+GPT4接口
  • 双目测距联合YOLOv8 项目总结
  • Windows提权:利用MSSQL数据库,Oracle数据库
  • linux常见的二十多个指令
  • 内蒙古自治区住房和城乡建设分析及解决方案
  • JavaEE进阶5/25(属性注入)