Created
March 3, 2012 23:09
-
-
Save alobato/1968852 to your computer and use it in GitHub Desktop.
start-stop-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
That's so helpful 👍
Nice and clean - thanks
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}"
Thanks, very helpful!
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
lovely, thanks!
@alobato
I guess add --make-pidfile like below
start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
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
!!! thx so much!