Make daemonize script more robust

This commit is contained in:
Martin Polden 2013-05-23 10:31:09 +02:00
parent 57a17166c9
commit ad6155e886
1 changed files with 22 additions and 24 deletions

View File

@ -1,47 +1,43 @@
#!/usr/bin/env bash #!/usr/bin/env bash
NAME="ifconfig" NAME="ifconfig"
IFC_PATH=$(dirname $(readlink -f $0)) PREFIX=$(dirname $(readlink -f $0))
LOCK_FILE="$IFC_PATH/tmp/${NAME}.lock" DAEMON="$PREFIX/$NAME"
PID_FILE="$IFC_PATH/tmp/${NAME}.pid" PID_FILE="${PREFIX}/tmp/${NAME}.pid"
LOG_FILE="$IFC_PATH/tmp/${NAME}.log" LOCK_FILE="${PREFIX}/tmp/${NAME}.lock"
BIN="$IFC_PATH/$NAME" LOG_FILE="${PREFIX}/tmp/${NAME}.log"
E_USAGE=1 E_USAGE=1
E_NOTFOUND=2 E_NOTFOUND=2
E_NOPID=3
E_LOCKED=4
if [[ ! -x "$BIN" ]]; then if [[ ! -x "$DAEMON" ]]; then
echo "$BIN does not exist or is not executable" echo "$DAEMON does not exist or is not executable"
exit $E_NOTFOUND exit $E_NOTFOUND
fi fi
start () { start () {
if [[ -f "$LOCK_FILE" ]]; then echo -n "Starting $NAME: "
echo "Lock file $LOCK_FILE exists. Already running?" mkdir -p $PREFIX/tmp
exit $E_LOCKED daemonize -c $PREFIX -o $LOG_FILE -p $PID_FILE -l $LOCK_FILE $DAEMON && \
fi echo "ok" || echo "failed"
echo "Starting $NAME"
daemonize -c $IFC_PATH -o $LOG_FILE -p $PID_FILE -l $LOCK_FILE $BIN
} }
stop () { stop () {
if [[ ! -s "$PID_FILE" ]]; then echo -n "Stopping $NAME: "
echo "PID file $PID_FILE empty or not found. Not started?" if [[ -s "$PID_FILE" ]]; then
exit $E_NOPID PID=$(head -n1 $PID_FILE)
kill $PID 2> /dev/null && echo "ok" || echo "not running?"
fi fi
PID=$(head -n1 $PID_FILE)
echo "Stopping $NAME: $PID"
kill $PID
rm -f -- $PID_FILE $LOCK_FILE rm -f -- $PID_FILE $LOCK_FILE
} }
status () { status () {
if [[ ! -s "$PID_FILE" ]]; then if [[ -s "$PID_FILE" ]]; then
echo "$NAME is not running" PID=$(head -n1 $PID_FILE)
kill -0 $PID 2> /dev/null && echo "$NAME is running (pid: $PID)" || \
echo "$NAME is not running"
else else
echo "$NAME is running: $(head -n1 $PID_FILE)" echo "$NAME is not running"
fi fi
} }
@ -64,3 +60,5 @@ case "$1" in
exit $E_USAGE exit $E_USAGE
;; ;;
esac esac
exit $?