Linux crond和crontab

数据管理中经常遇到数据备份、迁移操作, 这些操作往往是需要周期性的执行。

Linux 下有crond,crontab 工具可以很好的帮助我们

crond,一般情况下,系统都会默认启动次调度服务,crond会每分钟检查需要定时执行的操作。

crontab,顾名思义,定时任务列表,通过crontab来配置定时任务

usage:    crontab [-u user] file
    crontab [-u user] [ -e | -l | -r ]
        (default operation is replace, per 1003.2)
    -e    (edit user's crontab)
    -l    (list user's crontab)
    -r    (delete user's crontab)
    -i    (prompt before deleting user's crontab)
    -s    (selinux context)

常见用法

 sudo crontab -e -u xddy

配置用户xddy的定时任务

crontab 配置方式

一行一个定时任务

MIN HOUR DAY MONTH DAYOFWEEK   COMMAND

* * * * *  cmd  

* 默认表示执行,如果都是*则相当于每分钟执行一次

当 MIN 为 a, b, c,... 时表示第 a, b, c,... 分钟要执行,HOUR为 a, b, c,... 时表示第 a, b, c...个小时要执行 ..

* * * * * cd /home/xddy/switch/bin && bash taskSwitchDB.sh >> ../log/task.log 2>&1
0 2 * * * cd /home/xddy/monitor && bash clearHistoryTasks.sh 24 >> removed.log 2>&1
* * * * * cd /home/xddy/switch/bin && bash dbsUpdate.sh >> ../log/zk.log 2>&1
0 2 * * * cd /home/xddy/monitor && bash clearHistoryTasks.sh 24 >> removed.log 2>&1
表示每天凌晨2点钟执行一次 bash clearHistoryTasks.sh 24
0 */2 * * * 表示每隔两小时执行一次
0 23-7/2 * * * 每天23点到第二天7点这期间,每隔2小时
5,15,25,35,45,55 * * * 6 每周六每小时的第5,15,25,35,45,55分钟

 crond只能每分钟扫描一次, 如果想精确到秒,可以在脚本中执行

设置定时任务 * * * * * 每分钟执行一次,在脚本中执行sleep seconds 

或者配置定时任务  sleep 1 && test.sh  。。感觉有点挫。

或者干脆不用crontab,直接运行一个后台脚本,每隔多少时间执行一次操作。

原文地址:https://www.cnblogs.com/xddy/p/3236085.html