Skip to content

Instantly share code, notes, and snippets.

@wware
Last active September 14, 2016 13:05
Show Gist options
  • Save wware/cca3b0c1758156de24d49a883bb2a360 to your computer and use it in GitHub Desktop.
Save wware/cca3b0c1758156de24d49a883bb2a360 to your computer and use it in GitHub Desktop.

I need a reminder about how to set up a service in Linux that will run a script at boot time. Put this LSB init script in your /etc/init.d directory and then type

sudo update-rc.d linux_service.sh defaults

to set up the service. This is a wrapper for the script you actually want to run. The "Required-Start" section allows you to choose what Linux services need to be running before your own service is started. (I think my Fluidsynth script probably wants "alsasound".)

Some relevant links about this stuff:

#! /bin/sh
### BEGIN INIT INFO
# Provides: hack
# Required-Start: $syslog
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: An example of how to set up a service.
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin
. /lib/lsb/init-functions
SCRIPT=/some/script/to/run/at/startup
PIDFILE=/var/run/hack.pid
start() {
log_daemon_msg "Starting hack service"
if [ -f $SCRIPT ]; then
$SCRIPT &
if [ "$?" != "0" ]; then
log_failure_msg "The script failed"
else
echo $! > $PIDFILE
fi
fi
}
stop() {
log_daemon_msg "Stopping hack service"
if [ -f $PIDFILE ]; then
kill -9 `cat $PIDFILE`
rm -f $PIDFILE
fi
}
case "$1" in
start)
start
exit 0
;;
stop)
stop
exit 0
;;
restart)
stop
start
exit 0
;;
*)
echo "Usage: /etc/init.d/hack.sh {start|stop|restart}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment