🎉 Happy News! The new website is out :-) Google

bpkg-pid

bpkg-pid

Create, read, check and remove PID files for bash daemons. Prevents double-start races and makes process management predictable.

Installation

bpkg install stephenmathieson/bpkg-pid

Usage

source bpkg_modules/bpkg-pid/pid.sh

PIDFILE=/var/run/myscript.pid

# Write current PID
pid_write "$PIDFILE"

# Check if process is running
if pid_running "$PIDFILE"; then
  echo "already running, exit"
  exit 1
fi

# Read PID value
pid=$(pid_read "$PIDFILE")

# Remove PID file on exit
trap "pid_remove $PIDFILE" EXIT

Functions

  • pid_write — write $$ to file
  • pid_read — read PID from file
  • pid_running — check if PID file exists and process is alive
  • pid_remove — delete PID file if it belongs to current process
  • pid_lock — atomic test-and-set (race-free)