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

ajk*_*ush 3 linux shell cron synchronization crontab

我有一个shell脚本在crontab其中执行jar文件.Jar文件正在将文件从一台服务器移动到另一台服 在高峰时段,它需要超过10分钟(更多的crontab条目).

如何cron在最后一个工作完成之前确保作业不会执行?

Ale*_*lex 6

一个简单的方法是让您的Cron启动一个bash文件来检查是否存在这样的进程.

cron的:

 */10 * * * * /path/to/bashscript.sh
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

- 编辑 -

灵感来自F. Hauri的答案(工作正常),我想出了一个更短的版本:

 */10 * * * *  pgrep -n "yourjarfile." || /usr/bin/java -jar /path/to/yourjarfile.jar
Run Code Online (Sandbox Code Playgroud)