我打赌有人已经解决了这个问题,也许我正在使用错误的谷歌搜索条件告诉我答案,但这是我的情况。
我有一个我想要运行的脚本,但我希望它只在计划时运行,一次只运行一个。 (无法同时运行脚本)
现在粘性部分就是说我有一个名为“myhappyschedule”的表,它有我需要的数据和预定的时间。此表甚至可以同时具有多个计划时间,每个表都可以运行此脚本。所以基本上我需要每次脚本触发时都有一个队列,他们都需要等待每个脚本才能完成。 (有时这可能需要一分钟才能让脚本有时执行很多分钟)
我正在考虑做的是创建一个脚本,每隔5分钟检查一次myhappyschedule并收集那些已调度的脚本,将它们放入队列,其中另一个脚本可以按顺序执行队列中的每个“作业”或事件。所有这一切听起来都很混乱。
为了延长时间 - 我应该说我允许用户在myhappyschedule中安排内容而不是编辑crontab。
可以做些什么呢?文件锁和脚本调用脚本?
答案 0 :(得分:4)
将exec_status
列添加到myhappytable
(也可能是time_started
和time_finished
,请参阅伪代码
每隔x分钟运行以下cron脚本
cron脚本的伪代码:
[create/check pid lock (optional, but see "A potential pitfall" below)]
get number of rows from myhappytable where (exec_status == executing_now)
if it is > 0, exit
begin loop
get one row from myhappytable
where (exec_status == not_yet_run) and (scheduled_time <= now)
order by scheduled_time asc
if no such row, exit
set row exec_status to executing_now (maybe set time_started to now)
execute whatever command the row contains
set row exec_status to completed
(maybe also store the command output/return as well, set time_finished to now)
end loop
[delete pid lock file (complementary to the starting pid lock check)]
这样,脚本首先检查是否所有命令都在运行,然后先运行not-yet run命令,直到在给定时刻没有其他命令运行。此外,您可以通过查询数据库来查看正在执行的命令。
潜在的陷阱:如果cron脚本被终止,则计划任务将保持“execution_now”状态。这就是开始和结束时的pid锁定:查看cron脚本是否正确终止。 create / check pidlock的伪代码:
if exists pidlockfile then
check if process id given in file exists
if not exists then
update myhappytable set exec_status = error_cronscript_died_while_executing_this
where exec_status == executing_now
delete pidlockfile
else (previous instance still running)
exit
endif
endif
create pidlockfile containing cron script process id
答案 1 :(得分:2)
您可以在脚本中使用at(1)命令来安排下次运行。在退出之前,它可以检查myhappyschedule以便下次运行。你根本不需要cron。
答案 2 :(得分:0)
我在研究排队问题的解决方案时遇到了这个问题。为了其他任何人的利益,我的解决方案是我的解决方案。
将它与在计划时启动作业的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
在这种情况下,初始调用将启动阅读器并以single_instance_script
作为参数运行some arguments
。一旦呼叫完成,读者将从管道中获取下一个请求并执行some other_arguments
,完成,抓住下一个等...
此脚本将阻止请求进程,因此将其作为后台作业(&amp; at the end)或作为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