Skip to content

Instantly share code, notes, and snippets.

@chilicat
Last active December 22, 2015 12:39
Show Gist options
  • Save chilicat/6474191 to your computer and use it in GitHub Desktop.
Save chilicat/6474191 to your computer and use it in GitHub Desktop.
A example rhel service init script.
#!/bin/bash
### BEGIN INIT INFO
# Provides: test
# Required-Start: postgresql networking
# Required-Stop: postgresql networking
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: test
# Description: test
### END INIT INFO
. /etc/init.d/functions
NAME=test
PID_FILE="/var/run/${NAME}.pid"
DELAY=0
# only usable for root
[ $EUID = 0 ] || exit 4
status_q() {
status -p $PID_FILE $NAME > /dev/null
return $?
}
start_app() {
nohup node /tmp/test.js >/dev/null 2>&1 </dev/null &
return $?
}
check_config() {
# Do not start if there is no config file.
# [ ! -f "$CONFIG_FILE" ] && return 6
return 0
}
start() {
status_q; res=$?
if [ $res -eq 3 ]; then
echo -n $"$NAME starting..."
start_app
echo $! > $PID_FILE
[ $DELAY -gt 0 ] && sleep $DELAY
status_q; res=$?
[ $res -eq 0 ] && success || failure
echo
elif [ $res -eq 0 ]; then
success; echo "$NAME is already running"
else
failure; echo "$NAME is not running, res: $res"
fi
return $res
}
stop() {
status_q; res=$?
if [ $res -eq 3 ]; then
success && echo "$NAME is not running"
else
killproc -p $PID_FILE; res=$?
if [ $res -eq 0 ]; then
rm -f PID_FILE
success && echo "$NAME has been stopped"
else
failure && echo "$NAME cannot be stopped ($res)"
fi
fi
return $res
}
case $1 in
start)
start
exit $?
;;
stop)
stop
exit $?
;;
status)
echo -n $"$NAME running..."
status_q; res=$?
[ $res -eq 0 ] && success || failure
echo
exit $res
;;
restart)
stop
start
exit $?
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment