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 ?
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With