Created
September 15, 2016 21:18
-
-
Save omar-yassin/9b2cdbf0a7aaf80f74350f2c8d9f832e to your computer and use it in GitHub Desktop.
shell init deamon skeleton
This file contains hidden or 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/bash | |
| # chkconfig: 345 99 01 | |
| # description: ${SOME_NAME} | |
| . /etc/rc.d/init.d/functions | |
| RUNAS=omar | |
| NAME='loop_test' | |
| SCRIPT='/home/omar/while_loop.sh' | |
| LOG_FILE="/home/omar/${NAME}.log" | |
| PID_FILE='/tmp/loop.pid' | |
| status_check() { | |
| # exit codes | |
| #0 program is running or service is OK | |
| #1 program is dead and /var/run pid file exists | |
| #2 program is dead and /var/lock lock file exists | |
| #3 program is not running | |
| status -p ${PID_FILE} ${NAME} | |
| } | |
| status_check_silent() { | |
| status_check >/dev/null 2>&1 | |
| } | |
| start() { | |
| echo -n $"Starting ${NAME}: " | |
| su - ${RUNAS} -c 'nohup '${SCRIPT}' > '${LOG_FILE}' 2>&1 & echo "$!"' > ${PID_FILE} | |
| echo "${NAME} is running on PID `cat ${PID_FILE}`" | |
| } | |
| stop() { | |
| PID=`cat ${PID_FILE}` | |
| echo -n $"Stopping ${NAME}: (PID ${PID}) " | |
| kill -9 "${PID}" | |
| status_check_silent | |
| retval=$? | |
| if [[ retval -eq 1 ]] ; then | |
| echo "${NAME} killed and now removing pid" | |
| rm -f "${PID_FILE}" | |
| else | |
| echo " * [ERROR] Failed to kill service ${NAME}" | |
| exit 2 | |
| fi | |
| } | |
| case "$1" in | |
| start) | |
| status_check_silent && exit 0 | |
| $1 | |
| ;; | |
| stop) | |
| status_check_silent || exit 0 | |
| $1 | |
| ;; | |
| restart) | |
| stop | |
| sleep 5 | |
| start | |
| ;; | |
| status) | |
| status_check | |
| ;; | |
| *) | |
| echo "Usage: $0 {start|stop|status|restart}" | |
| exit 2 | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment