linux中crontab每秒執行任務的幾種方法

來源:互聯網
上載者:User

第一種方法:

1.建立指令碼檔案

 代碼如下 複製代碼
cat phplog.sh
2.編輯指令碼內容
#!/bin/bash
while : ;do
/home/scripts.sh 2>/dev/null &
sleep 1
done

注意:第一次運行時請不要使用bash phplog.sh & 這種後台啟動並執行方式,它會僵死的。
解決:可以把它放到計劃任務使其運行,然後將計劃任務中的此條目刪除即可。最後把這個指令碼放到/etc/rc.local讓它每次開機都可以被運行。

第二種方法:

 代碼如下 複製代碼

1.建立指令碼檔案
cat phplog.sh
2.編輯指令碼內容
#!/bin/bash
for((i=1;i<=20;i++));do
/home/scripts.sh 2>/dev/null &
sleep 1
done
3.編輯crontab指令碼
* * * * * /bin/bash /home/somedir/phplog.sh

第三種方法:

1.編輯crontab指令碼

 代碼如下 複製代碼
* * * * * /home/scripts.sh
* * * * * sleep 1 && /home/scripts.sh
* * * * * sleep 2 && /home/scripts.sh
* * * * * sleep 3 && /home/scripts.sh
…
* * * * * sleep 57 && /home/scripts.sh
* * * * * sleep 58 && /home/scripts.sh
* * * * * sleep 59 && /home/scripts.sh

最後推薦使用第三種方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.