How to prevent the cron job execution, if it is already running
Advisory locking is made for exactly this purpose.
You can accomplish advisory locking with flock()
. Simply apply the function to a previously opened lock file to determine if another script has a lock on it.
$f = fopen('lock', 'w') or die ('Cannot create lock file');
if (flock($f, LOCK_EX | LOCK_NB)) {
// yay
}
In this case I'm adding LOCK_NB
to prevent the next script from waiting until the first has finished. Since you're using cron there will always be a next script.
If the current script prematurely terminates, any file locks will get released by the OS.
Run cron job only if it isn't already running
I do this for a print spooler program that I wrote, it's just a shell script:
#!/bin/sh
if ps -ef | grep -v grep | grep doctype.php ; then
exit 0
else
/home/user/bin/doctype.php >> /home/user/bin/spooler.log &
#mailing program
/home/user/bin/simplemail.php "Print spooler was not running... Restarted."
exit 0
fi
It runs every two minutes and is quite effective. I have it email me with special information if for some reason the process is not running.
Stop an already running cron job
http://unix.derkeiler.com/Newsgroups/comp.unix.admin/2006-09/msg00132.html
http://unix.ittoolbox.com/groups/technical-functional/shellscript-l/how-to-kill-the-cronjob-which-is-running-currently-477250
You need to get the PID of your running cron job and then perform simple kill command.
Stop cron job from starting while it's already running (even across servers)
A PID file over a shared folder?
Schedule cron entries to run script only when not already running
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
How to prevent a Cronjob execution in Kubernetes if there is already a job running
CronJob resource has a property called concurrencyPolicy
, here an example:
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: your-cron
spec:
schedule: "*/40 8-18 * * 1-6"
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
metadata:
labels:
app: your-periodic-job
spec:
containers:
- name: your_container
image: your_image
imagePullPolicy: IfNotPresent
restartPolicy: OnFailure
Related Topics
Programmatically Access Currency Exchange Rates
Print Newline in PHP in Single Quotes
How to Prevent the Cron Job Execution, If It Is Already Running
Any Way to Specify Optional Parameter Values in PHP
How to Set Utf-8 Encoding for a PHP File
How to Set, Get and Destroy Cookies in Wordpress
Pdostatement (Mysql): Inserting Value 0 into a Bit(1) Field Results in 1 Written in Table
MySQL Select Query Within a Serialized Array
Single and Double Quotes Together as HTML Attribute Value
Fastest Way to Add Prefix to Array Keys
Notice: Unknown: Skipping Numeric Key 1 in Unknown on Line 0
Remove Diacritics from a String
How to Place Two Forms on the Same Page
Curl and Https, "Cannot Resolve Host"
Ajax Post Request in Laravel 5 Return Error 500 (Internal Server Error)
Trim Unicode Whitespace in PHP 5.2
How to Use Single Quote Inside an Echo Which Is Using Single Quote