Bash
每 X 秒執行一次命令
我希望每 10 秒執行一次命令,並讓它在後台執行(從而消除
watch
?)。所有答案都顯示如下,但這將執行 11 到 14 秒。如何實現?while true; do # perform command that takes between 1 and 4 seconds sleep 10 done
怎麼樣:
( # In a subshell, for isolation, protecting $! while true; do perform-command & # in the background sleep 10 ; ### If you want to wait for a perform-command ### that happens to run for more than ten seconds, ### uncomment the following line: # wait $! ; ### If you prefer to kill a perform-command ### that happens to run for more than ten seconds, ### uncomment the following line instead: # kill $! ; ### (If you prefer to ignore it, uncomment neither.) done )
ETA:有了所有這些評論、替代方案和額外保護的子shell,這看起來比一開始要復雜得多。
wait
所以,為了比較,這是我開始擔心or之前的樣子kill
,他們$!
需要隔離:while true; do perform-command & sleep 10 ; done
其餘的只是在您需要時使用。