简体   繁体   English

安排cron条目仅在尚未运行时运行脚本

[英]Schedule cron entries to run script only when not already running

I have one shell script in crontab which is executing jar file. 我在crontab有一个执行jar文件的shell脚本。 Jar file is moving files from one server to another server. Jar文件正在将文件从一台服务器移动到另一台服 In peak hours it is taking more then 10 minutes(more then crontab entries). 在高峰时段,它需要超过10分钟(然后是crontab条目)。

How can I make sure that cron job will not execute process until last one is not completed ? 如何确保cron作业在最后一个未完成之前不会执行进程?

An easy way would be to have your Cron start a bashfile that checks if such a process exist. 一个简单的方法是让您的Cron启动一个bash文件来检查是否存在这样的进程。

cron: cron的:

 */10 * * * * /path/to/bashscript.sh

(Make sure it has the correct user and is executable) (确保它具有正确的用户并且是可执行的)

The pgrep command looks for a process with a the given name, and returns the processID when such a process is found. pgrep命令查找具有给定名称的进程,并在找到此进程时返回processID。

#!/bin/bash
# bashscript.sh    

pID=$(pgrep -n "yourjarfile") 

# Check if jarfile is running
if $pID > /dev/null
then       
    #log something to syslog 
    logger  $pID "already running. not restarting." 
else
    # start jar file 
    /usr/bin/java -jar /path/to/yourjarfile.jar
fi

--EDIT-- - 编辑 -

Inspired by F. Hauri's answer (which works fine btw), I came up with a shorter version : 灵感来自F. Hauri的答案(工作正常),我想出了一个更短的版本:

 */10 * * * *  pgrep -n "yourjarfile." || /usr/bin/java -jar /path/to/yourjarfile.jar

您可以直接在cron条目中使用穷人的锁文件,例如:

* * * * * LOCKFILE=whatever; [ ! -f $LOCKFILE ] && (touch $LOCKFILE && /path/to/java javaargs ; rm $LOCKFILE)

Crontab can't control script execution to make what you want, you must use something else. Crontab无法控制脚本执行来制作你想要的东西,你必须使用别的东西。 The simpler way I can think of doing it is by creating and monitoring a file containing the status of the executing task. 我能想到的更简单的方法是创建和监视包含执行任务状态的文件。 First of all create a control file under the same directory as the bash script that calls Java doing: $echo 0 > javaisrun.txt 首先在与调用Java的bash脚本相同的目录下创建一个控制文件:$ echo 0> javaisrun.txt

After this you can have the own script checking if Java is running or not and take the appropriated response doing something like: 在此之后,您可以使用自己的脚本检查Java是否正在运行,并采取适当的响应,例如:

#!/bin/bash

A=$(cat javaisrun.txt)

if [ $A = 1 ]; then
echo Java is running already
else
echo Not Running, start Java
echo 1 > javaisrun.txt
java -jar temp3.jar
echo Java job is done, restoring state file
echo 0 > javaisrun.txt
fi

Script will read the 'javaisrun.txt' file, take its value and decide to go ahead with Java or not doing the update on the file while Java is executing and after execution. 脚本将读取'javaisrun.txt'文件,获取其值并决定继续使用Java,或者在Java执行时和执行后不对文件执行更新。

I use sometime this kind of trick: 我有时会使用这种技巧:

*/10 * * * * ps -C javaws www| grep -q mytool.jar || javaws ... mytool.jar

Not very sexy, but efficient. 不是很性感,但很有效率。

I use an approach that is similar to Rodrigo's answer but also relegates semaphore cleanup to cron. 我使用的方法类似于罗德里戈的答案,但也将信号量清理降级为cron。 Something like: 就像是:

* * * * *  cd workdir; S=<a lockfile name>; touch $S; myjob jobparams; rm $S

This way if somehow the job crashed or someone killed it, cron will take care of removing the lock file. 这样,如果某个工作崩溃或有人杀死它,cron将负责删除锁定文件。 It is not foolproof but has worked well for me. 它不是万无一失,但对我来说效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM