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 filepid_read— read PID from filepid_running— check if PID file exists and process is alivepid_remove— delete PID file if it belongs to current processpid_lock— atomic test-and-set (race-free)