Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- #: Filename : runtool.sh
- #: Title : runtool
- #: Author : "Iarmin" <[email protected]>
- #: Version : 0.1
- #: Description : Adding service like control to your scripts. Easy to implement.
- #: Dependency : none
- #: Usage : Place the following code into the beginning of your script
- #
- # PIDFILE="/tmp/yourprogram.lock" # the pidfile path
- # source ./runtool.sh
- #
- # # ... and here comes your code
- #
- runtools_usage() {
- echo "Usage: $0 [start|stop|restart|status}"
- exit 1
- }
- process_exists() {
- if [ -n "$1" ] && [ -d "/proc/$1" ]; then
- return 0
- else
- return 1
- fi
- }
- if [ -z "$1" ]; then
- runtools_usage
- fi
- if [ -z "$PIDFILE" ]; then
- echo "No \$PIDFILE set"
- exit 2
- fi
- case "$1" in
- "start")
- echo 'Starting...'
- if process_exists "$(cat $PIDFILE 2> /dev/null)"; then
- echo " Already running!"
- exit 3
- fi
- ( $0 daemon & ) &> /dev/null
- exit 0
- ;;
- "stop")
- echo 'Stopping...'
- if process_exists "$(cat $PIDFILE 2> /dev/null)" ; then
- _PID="$(cat $PIDFILE 2> /dev/null)"
- kill "$_PID"
- sleep 1
- if process_exists "$_PID"; then
- kill -9 "$_PID"
- fi
- exit 0
- else
- echo " Not running!"
- exit 4
- fi
- exit 0
- ;;
- "restart")
- $0 stop
- sleep 1
- $0 start
- exit 0
- ;;
- "daemon")
- DAEMON=1
- echo "$$" > "$PIDFILE"
- ;;
- "status")
- if process_exists "$(cat $PIDFILE 2> /dev/null)"; then
- echo "running: $(cat $PIDFILE 2> /dev/null)"
- else
- echo "not running"
- fi
- exit 0
- ;;
- *)
- runtools_usage
- ;;
- esac
- if [ "$DAEMON" != "1" ]; then
- exit
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement