Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RichardBronosky/b037de3e8763887b034298057200bd02 to your computer and use it in GitHub Desktop.
Save RichardBronosky/b037de3e8763887b034298057200bd02 to your computer and use it in GitHub Desktop.
start-stop-daemon-example
#!/bin/bash -e
# Quick start-stop-daemon example, derived from Debian /etc/init.d/ssh
NAME=blink
DIR=/home/pi
PIDFILE=/home/pi/$NAME.pid
DAEMON=/home/pi/pi_gpio_hack.py
DAEMON_ARGS="[[31,0,37,1],[31,1,37,0]]"
STOP_SIGNAL=INT
USER=pi
LOG=/home/pi/$NAME.log
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
common_opts="--quiet --chuid $USER --pidfile $PIDFILE"
do_start(){
start-stop-daemon --start $common_opts --chdir $DIR --make-pidfile --background --startas \
/bin/bash -- -c "exec $DAEMON $DAEMON_ARGS > $LOG 2>&1"
}
do_stop(){
opt=${@:-}
start-stop-daemon --stop $common_opts --signal $STOP_SIGNAL --oknodo $opt --remove-pidfile
}
do_status(){
start-stop-daemon --status $common_opts && exit_status=$? || exit_status=$?
echo asdf $exit_status
case "$exit_status" in
0)
echo "Program '$NAME' is running."
;;
1)
echo "Program '$NAME' is not running and the pid file exists."
;;
3)
echo "Program '$NAME' is not running."
;;
4)
echo "Unable to determine program '$NAME' status."
;;
esac
}
case "$1" in
status)
do_status
;;
start)
echo -n "Starting daemon: "$NAME
do_start
echo "."
;;
stop)
echo -n "Stopping daemon: "$NAME
do_stop
echo "."
;;
restart)
echo -n "Restarting daemon: "$NAME
do_stop --retry 30
do_start
echo "."
;;
*)
echo "Usage: "$1" {status|start|stop|restart}"
exit 1
esac
exit 0
@k3b4bb
Copy link

k3b4bb commented Feb 12, 2024

You have just saved my life. I got my dpkg start-stop-daemon corrupted or deleten (idk i can't locate it in the whole system) and with your example I could write my own one and get my linux system working properly after 2 days. Thanks man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment