I am looking for best practice for ensuring a script executed by a cron job every minute only has one running instance. For e.g. if I have a cron that executed every minute and in case the process takes longer then one minute then do not execute another till done.
For now I have the below function. In essence I get the name of the current process and I do a ps grep to see if the count of the current process is listed. Kinda messy so I was looking for a more pythonic way.
I place the code on top of a file. It does work but again messy.
def doRunCount(stop=False,min_run=1):
import inspect
current_file = inspect.getfile( inspect.currentframe() )
print current_file
fn = current_file.split()
run_check = os.popen('ps aux | grep python').read().strip().split('\n')
run_count = 0
for i in run_check:
if i.find('/bin/sh')<0:
if i.find(current_file)>=0:
run_count = run_count + 1
if run_count>min_run:
print 'max proccess already running'
exit()
return run_count