linux - Schedule cron entries to run script only when not already running -
i have 1 shell script in crontab
executing jar file. jar file moving files 1 server server. in peak hours taking more 10 minutes(more crontab
entries).
how can make sure cron
job not execute process until last 1 not completed ?
an easy way have cron start bashfile checks if such process exist.
cron:
*/10 * * * * /path/to/bashscript.sh
(make sure has correct user , executable)
the pgrep
command looks process given name, , returns processid when such process found.
#!/bin/bash # bashscript.sh pid=$(pgrep -n "yourjarfile") # check if jarfile running if $pid > /dev/null #log syslog logger $pid "already running. not restarting." else # start jar file /usr/bin/java -jar /path/to/yourjarfile.jar fi
--edit--
inspired f. hauri's answer (which works fine btw), came shorter version :
*/10 * * * * pgrep -n "yourjarfile." || /usr/bin/java -jar /path/to/yourjarfile.jar