Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
start-stop-example
#!/bin/sh
# Quick start-stop-daemon example, derived from Debian /etc/init.d/ssh
set -e
# Must be a valid filename
NAME=foo
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname
DAEMON=/usr/local/bin/bar
DAEMON_OPTS="--baz=quux"
export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"
case "$1" in
start)
echo -n "Starting daemon: "$NAME
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
echo "."
;;
stop)
echo -n "Stopping daemon: "$NAME
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
echo "."
;;
restart)
echo -n "Restarting daemon: "$NAME
start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
echo "."
;;
*)
echo "Usage: "$1" {start|stop|restart}"
exit 1
esac
exit 0
@SkyzohKey

This comment has been minimized.

Copy link

commented Apr 22, 2016

Thanks for the sample, you saved my day. :)

@leetschau

This comment has been minimized.

Copy link

commented May 12, 2016

good work!

@maritaria

This comment has been minimized.

Copy link

commented May 21, 2016

Awesome

@anthonyraymond

This comment has been minimized.

Copy link

commented May 25, 2016

Thanks 😉

@silvestrefigueroa

This comment has been minimized.

Copy link

commented Jun 30, 2016

!!! thx so much!

@iamalirezaj

This comment has been minimized.

Copy link

commented Dec 28, 2016

That's so helpful 👍

@gsmcfie

This comment has been minimized.

Copy link

commented Aug 15, 2017

Nice and clean - thanks

@fvargasruiz

This comment has been minimized.

Copy link

commented Oct 4, 2017

Good example ... but, what's about the "status" option?

In the default option of "case" I think you have to use $0 instead of $1:
echo "Usage: "$0" {start|stop|restart}"

@chadmccathie

This comment has been minimized.

Copy link

commented Nov 2, 2017

Thanks, very helpful!

@TopperBG

This comment has been minimized.

Copy link

commented Feb 25, 2018

I think that could work if runs only with root/sudo
With localuser that
PIDFILE=/var/run/$NAME.pid
will not work, proper way will be
PIDFILE=/var/run/$NAME/$NAME.pid
/var/run is writable only for root

@vsee

This comment has been minimized.

Copy link

commented Mar 1, 2018

lovely, thanks!

@Eberx

This comment has been minimized.

Copy link

commented Mar 6, 2018

@alobato
I guess add --make-pidfile like below
start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS

@nmvega

This comment has been minimized.

Copy link

commented Nov 28, 2018

I couldn't find a way to +1 @fvargasruiz comment, but he's correct so I'm quoting him here:

In the default option of "case" I think you have to use $0 instead of $1:
echo "Usage: "$0" {start|stop|restart}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.