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
相關文章
相關標籤/搜索