我在研究排隊問題的解決方案時遇到了這個問題。爲了在這裏搜索的任何人的利益是我的解決方案。
將此功能與按計劃啓動作業的cron結合使用(即使它們計劃同時運行),也解決了您所描述的問題。
問題
- 在腳本的最一個實例應該運行。
- 我們希望提出儘可能快的處理請求。
即,我們需要一個通往腳本的管道。
解決方法:
創建一個管道的任何腳本。完成使用一個小bash腳本(進一步下來)。
該腳本可稱爲
./pipeline "<any command and arguments go here>"
實施例:
./pipeline sleep 10 &
./pipeline shabugabu &
./pipeline single_instance_script some arguments &
./pipeline single_instance_script some other_argumnts &
./pipeline "single_instance_script some yet_other_arguments > output.txt" &
..etc
的腳本創建爲每個命令一個新named pipe。所以上面將創建命名管道:sleep
,shabugabu
和single_instance_script
在這種情況下,最初的通話將開始一個閱讀器,並與some arguments
作爲參數運行single_instance_script
。一旦調用完成,讀者會搶掉管道中的下一個請求,並與some other_arguments
執行,完成,搶下等等
這個腳本會阻塞請求進程,以便把它作爲後臺作業(&在結束)或帶at
(at now <<< "./pipeline some_script"
分離的進程)
#!/bin/bash -Eue
# Using command name as the pipeline name
pipeline=$(basename $(expr "$1" : '\(^[^[:space:]]*\)')).pipe
is_reader=false
function _pipeline_cleanup {
if $is_reader; then
rm -f $pipeline
fi
rm -f $pipeline.lock
exit
}
trap _pipeline_cleanup INT TERM EXIT
# Dispatch/initialization section, critical
lockfile $pipeline.lock
if [[ -p $pipeline ]]
then
echo "$*" > $pipeline
exit
fi
is_reader=true
mkfifo $pipeline
echo "$*" > $pipeline &
rm -f $pipeline.lock
# Reader section
while read command < $pipeline
do
echo "$(date) - Executing $command"
($command) &> /dev/null
done
好吧..這是我沒有想到的。我想知道腳本運行時預定的時間是否可以通過。假設它在上午8:00執行,並且腳本運行10分鐘,並且在運行開始和結束時間之間的任何時間都會丟失。 – user30413 2008-10-22 16:49:00
好點。您可能希望在編輯myhappyschedule的任何過程中操作atq。午餐時我必須考慮這個問題。 – 2008-10-22 17:07:25