Skip to content

Instantly share code, notes, and snippets.

@whisperity
Last active March 15, 2019 01:05
Show Gist options
  • Save whisperity/4573819 to your computer and use it in GitHub Desktop.
Save whisperity/4573819 to your computer and use it in GitHub Desktop.
Start arbitrary shell script as a background daemon from init.d
#!/bin/sh
### BEGIN INIT INFO
# Provides: scriptname
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# Short-Description: Script short description
# Description: Starts/Stops the script
### END INIT INFO
USER=root
PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin
DESC="description"
NAME=script_name
DAEMON=/bin/$NAME
DAEMON_NAME=$NAME
LOGFILE=/var/log/script.log
# Or you can use
# LOGFILE=/dev/null
# to supress logging.
SCRIPTNAME=/etc/init.d/$NAME
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
su -c "daemon --user=$USER --name=$DAEMON_NAME --output=$LOGFILE --command=$DAEMON" root
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
su -c "daemon --user=$USER --name=$DAEMON_NAME --stop" root
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment