国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

linux定時任務crontab 實現每秒執行一次的方法

2019-10-26 18:56:51
字體:
來源:轉載
供稿:網友

linux crontab 命令,最小的執行時間是一分鐘。如需要在小于一分鐘內重復執行,可以有兩個方法實現。

Cron 各項的描述

以下是 crontab 文件的格式:

{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}
o minute: 區間為 0 – 59
o hour: 區間為0 – 23
o day-of-month: 區間為0 – 31
o month: 區間為1 – 12. 1 是1月. 12是12月.
o Day-of-week: 區間為0 – 7. 周日可以是0或7.

1.使用延時來實現每N秒執行

創建一個php做執行動作,非常簡單,就是把當前時間寫入log。

<?php file_put_contents('/home/fdipzone/php/crontab/run.log', date('Y-m-d H:i:s')."/r/n", FILE_APPEND); ?>

crontab -e 輸入以下語句,然后 :wq 保存退出。

* * * * * php /home/fdipzone/php/crontab/tolog.php * * * * * sleep 10; php /home/fdipzone/php/crontab/tolog.php * * * * * sleep 20; php /home/fdipzone/php/crontab/tolog.php * * * * * sleep 30; php /home/fdipzone/php/crontab/tolog.php * * * * * sleep 40; php /home/fdipzone/php/crontab/tolog.php * * * * * sleep 50; php /home/fdipzone/php/crontab/tolog.php 

使用 tail -f 查看執行情況,可以見到log每10秒被寫入一條記錄。

fdipzone@ubuntu:~$ tail -f /home/fdipzone/php/crontab/run.log 2014-03-31 21:47:01 2014-03-31 21:47:11 2014-03-31 21:47:21 2014-03-31 21:47:31 2014-03-31 21:47:41 2014-03-31 21:47:51 2014-03-31 21:48:01 

原理:通過延時方法 sleep N  來實現每N秒執行。

注意:

60必須能整除間隔的秒數(沒有余數),例如間隔的秒數是2,4,6,10,12等。

如果間隔的秒數太少,例如2秒執行一次,這樣就需要在crontab 加入60/2=30條語句。不建議使用此方法,可以使用下面介紹的第二種方法。

2.編寫shell腳本實現 推薦

crontab.sh

#!/bin/bash step=2 #間隔的秒數,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do   $(php '/home/fdipzone/php/crontab/tolog.php')   sleep $step done exit 0 

crontab -e 輸入以下語句,然后:wq 保存退出。

# m h dom mon dow  command * * * * * /home/fdipzone/php/crontab/crontab.sh 

使用 tail -f 查看執行情況,可以見到log每2秒被寫入一條記錄。

fdipzone@ubuntu:~/php/crontab$ tail -f run.log 2014-03-31 22:23:01 2014-03-31 22:23:03 2014-03-31 22:23:06 2014-03-31 22:23:08 2014-03-31 22:23:10 2014-03-31 22:23:12 2014-03-31 22:23:14 2014-03-31 22:23:16 2014-03-31 22:23:18 2014-03-31 22:23:20 2014-03-31 22:23:22 2014-03-31 22:23:25 2014-03-31 22:23:27 2014-03-31 22:23:29 2014-03-31 22:23:31 2014-03-31 22:23:33 2014-03-31 22:23:35 2014-03-31 22:23:37 2014-03-31 22:23:39 2014-03-31 22:23:41 2014-03-31 22:23:44 2014-03-31 22:23:46 2014-03-31 22:23:48 2014-03-31 22:23:50 2014-03-31 22:23:52 2014-03-31 22:23:54 2014-03-31 22:23:56 2014-03-31 22:23:58 2014-03-31 22:24:00             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遂平县| 信阳市| 汝州市| 南川市| 神池县| 兴安盟| 常山县| 佛学| 宜宾县| 长兴县| 临夏市| 彭泽县| 元谋县| 长泰县| 东方市| 长泰县| 平南县| 静宁县| 普兰店市| 博白县| 宜阳县| 乌拉特中旗| 清河县| 平顺县| 财经| 汉源县| 资源县| 景德镇市| 柳河县| 寿光市| 康平县| 滕州市| 营山县| 大城县| 梧州市| 龙川县| 叶城县| 崇礼县| 呈贡县| 石首市| 邳州市|