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

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


 

因为 Linux 的 crontab 的定时命令格式以下:linux

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

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

可是,因为实际应用中,可能须要每5秒就要求执行某个shell脚本。debug

该如何实现呢?code


 

本文中提供以下方式;blog

间隔调用shell命令

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

实现每5秒定时执行脚本,crontab 定时脚本以下(根据本身的实际状况,只须要将&& 后面的部分,替换成本身须要的脚本执行命令便可):get

*/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

 

执行效果以下:it


 

额外补充

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

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
相关文章
相关标签/搜索