Linux crontab 每5秒钟执行一次 shell 脚本 的方法

Linux crontab 每5秒钟执行一次 shell 脚本 的方法


由于 Linux 的 crontab 的定时命令格式如下:

 minute hour day-of-month month-of-year day-of-week commands

意味着标椎定时任务中,最小定时周期是分钟。

但是,由于实际应用中,可能需要每5秒就要求执行某个shell脚本。

该如何实现呢?


本文中提供如下方式;

间隔调用shell命令

使用 crontab -e 命令编辑定时任务

实现每5秒定时执行脚本,crontab 定时脚本如下(根据自己的实际情况,只需要将&& 后面的部分,替换成自己需要的脚本执行命令即可):

*/1 * * * * /bin/date >>/tmp/date.txt
*/1 * * * * sleep 5  && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 10 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 15 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 20 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 25 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 30 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 35 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 40 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 45 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 50 && /bin/date >>/tmp/date.txt
*/1 * * * * sleep 55 && /bin/date >>/tmp/date.txt

执行效果如下:


额外补充

crontab -l : 是查看当前 Linux 机器上的定时任务列表(llist 的首字母)

crontab -e : 是编辑定时任务(eedit 的首字母)

crontab 命令的具体使用说明如下:

Usage:
 crontab [options] file
 crontab [options]
 crontab -n [hostname]

Options:
 -u <user>  define user
 -e         edit user's crontab
 -l         list user's crontab
 -r         delete user's crontab
 -i         prompt before deleting
 -n <host>  set host in cluster to run users' crontabs
 -c         get host in cluster to run users' crontabs
 -s         selinux context
 -x <mask>  enable debugging

猜你喜欢

转载自www.cnblogs.com/miracle-luna/p/11832609.html