As the title says. I didn't have a sample, so I used it as a memo.
It is designed to do nothing if the process already exists. I think that this behavior is often preferable from cron.
import fcntl
lockfilePath = 'lockfile.lock'
with open(lockfilePath , "w") as lockFile:
try:
fcntl.flock(lockFile, fcntl.LOCK_EX | fcntl.LOCK_NB)
# Do SOMETHING
except IOError:
print('process already exists')
Recommended Posts