Skip to main content

Simple PID based locking for cronjobs, UNIX scripts or python programs

Project description


PyPI version CircleCI

Simple PID based locking for cronjobs, UNIX scripts or python programs. Copes with locking between hosts.


  • requires python3


  • Install with pip
sudo pip install -U pidlock
  • Use it from inside python script
import time
from pidlock import PIDLock

locker = PIDLock()
with locker.lock('sleepy_script'):
  • Use it as commandline/cron job
# To display help menu
pidlock -h    # Or pidlock --help

# Example usage
pidlock -n sleepy_script -c 'sleep 10'

# Same as
pidlock --name sleepy_script --command 'sleep 10'


  • You can pass PID file location, verbosity, time limit and minimum interval as arguments
# Python Usage
locker = PIDLock(lockdir='~/.pidlock', verbose=True)

with locker.lock('sleepy_script', wait=10, mininterval=1):
# Commandline usage
pidlock -n sleepy_script -c 'sleep 10' -l ~/.pidlock -v -w 10 -m 1

# Same as
pidlock --name sleepy_script --command 'sleep 10' --lockdir ~/.pidlock --verbose --wait 10 --mininterval 1


Originally written and published by Arijit Basu. Adaptation to cope with race conditions by Keiran Raine (ongoing owner/support).

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pidlock-3.0.2.linux-x86_64.tar.gz (7.2 kB view hashes)

Uploaded source

Built Distribution

pidlock-3.0.2-py3-none-any.whl (5.5 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page