找到你要的答案

Q:Schedule cron entries to run script only when not already running

Q:进度计划条目来运行脚本时没有运行

I have one shell script in crontab which is executing jar file. Jar file is moving files from one server to another server. In peak hours it is taking more then 10 minutes(more then crontab entries).

How can I make sure that cron job will not execute process until last one is not completed ?

我是一个crontab执行jar文件的shell脚本。jar文件正在将文件从一台服务器移动到另一台服务器。在高峰时间是以10分钟以上(超过crontab条目)。

我怎样才能确保cron作业将不执行过程直到最后一个没有完成?

answer1: 回答1:

An easy way would be to have your Cron start a bashfile that checks if such a process exist.

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.

#!/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 :

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

一个简单的方法可以让你的计划如果这样一个过程中存在启动bashfile检查。

cron:

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

(确保它有正确的用户并且是可执行的)

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

--编辑--

灵感来自F. Hauri的回答(优秀作品顺便说一句),我想出了一个较短的版本:

 */10 * * * *  pgrep -n "yourjarfile." || /usr/bin/java -jar /path/to/yourjarfile.jar
answer2: 回答2:

You can use a poor man's lockfile directly in the cron entry, something such as:

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

你可以在后台直接进入使用一个穷人的锁定文件,比如:

* * * * *锁定文件=什么;[!F $锁定文件] &;&;(触摸$ lockfile &;&;/路径/ java javaargs;人民币锁定文件)

answer3: 回答3:

Crontab can't control script execution to make what you want, you must use something else. 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

After this you can have the own script checking if Java is running or not and take the appropriated response doing something like:

#!/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.

crontab不能控制脚本的执行使你想要什么,你必须用别的。我能想到的简单方法是创建和监视包含执行任务状态的文件。首先创建一个控制文件的同目录下的bash脚本调用java做的:$ echo 0 & gt;javaisrun.txt

在这之后你能如果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

脚本将读取的javaisrun。txt”文件,把它的价值和决定继续java或不做更新的文件,java是执行和执行后。

answer4: 回答4:

I use sometime this kind of trick:

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

Not very sexy, but efficient.

我有时用这种伎俩:

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

不是很性感,而是效率。

answer5: 回答5:

I use an approach that is similar to Rodrigo's answer but also relegates semaphore cleanup to 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. It is not foolproof but has worked well for me.

我用这种方法类似于罗德里戈的回答也将信号清理计划。像:

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

这样,如果某种工作或有人杀了它坠毁,cron会照顾删除锁文件。这并不简单但一直对我很好。

linux  shell  cron  synchronization  crontab