Skip to content

Instantly share code, notes, and snippets.

@gurunars
Last active July 12, 2016 11:59
Show Gist options
  • Save gurunars/ff4b3f47e7c43a0a46e88e0d449a6686 to your computer and use it in GitHub Desktop.
Save gurunars/ff4b3f47e7c43a0a46e88e0d449a6686 to your computer and use it in GitHub Desktop.
Sample Linux service config for init.d
#!/bin/sh
# Put to /usr/bin/sample-program
sleep 600 &
PID="$!"
# Trap is required to kill all child processes when the service is stopped
trap "kill $PID" exit INT TERM KILL TERM
wait
#!/bin/bash
# processname: sample-program
# chkconfig: 345 99 5
### BEGIN INIT INFO
# Provides: sample-service
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Sample service
# Description: Sample service daemon
### END INIT INFO
# Put to /etc/init.d/sample-service
# Run: chmod +x /etc/init.d/sample-service
# Enable via: chkconfig --add sample-service
# Or via: update-rc.d sample-service defaults
# Manually available via "service start|stop|restart"
# More info here: https://wiki.debian.org/LSBInitScripts
PROG=sample-program
USER=sample-user
PID_FILE=/var/run/${PROG}.pid
LOG_FILE=/var/log/${PROG}.log
SUBSYS_FILE=/var/lock/subsys/${PROG}
start() {
if [ -f ${PID_FILE} ]; then
echo "${PROG} is already running"
else
touch ${LOG_FILE}
chown ${USER} ${LOG_FILE}
su ${USER} -c "nohup ${PROG} > ${LOG_FILE} 2>&1 & echo \$!" > ${PID_FILE}
touch ${SUBSYS_FILE}
fi
}
stop() {
if [ -f ${PID_FILE} ]; then
kill $(cat ${PID_FILE})
rm -f ${PID_FILE}
rm -f ${SUBSYS_FILE}
else
echo "${PROG} is not running"
fi
}
status() {
if [ -f ${PID_FILE} ]; then
echo "${PROG} is running"
else
echo "${PROG} is not running"
fi
}
case "${1}" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: ${0} {start|stop|status|restart}"
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment