Prevent double boot from cron in Python

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

Prevent double boot from cron in Python
Prevent double launch in Python (improved version)
OCR from PDF in Python
Extract text from images in Python
Extract strings from files in Python
Get exchange rates from open exchange rates in Python
What I got from Python Boot Camp
Download images from URL list in Python
Get battery level from SwitchBot in Python
Generate a class from a string in Python
Generate C language from S-expressions in Python
Convert from Markdown to HTML in Python
Get Precipitation Probability from XML in Python
Double pendulum equation of motion in python
Get metric history from MLflow in Python
Get time series data from k-db.com in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
From file to graph drawing in Python. Elementary elementary
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
sql from python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Load and execute command from yml in python
Use Python in your environment from Win Automation
Plink in Python
Constant in python
MeCab from Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Call a Python script from Embedded Python in C ++ / C ++
Create a datetime object from a string in Python (Python 3.3)
Load images from URLs using Pillow in Python 3
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Randomly select elements from list (array) in python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python