guneysus
5/15/2017 - 10:19 AM

example linux service

example linux service

#!/bin/bash
### BEGIN INIT INFO
# Provides:          testone
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     false
# Short-Description: Example init script
# Description:       Start/stop an example script

# 1. put a script /etc/init.d/exampleservice
# 2. install with running:
# sudo update-rc.d exampleservice defaults

cat << EOF
http://stackoverflow.com/a/22336712/1766716
the most common way to do that on debian/ubuntu is to build an initscript 
and place it in /etc/init.d or /etc/rc/init.d and place a script named mytestprogram in that.
EOF

### END INIT INFO

DESC="test script"
NAME=example
#DAEMON=

mkdir -p /var/log/example/

do_start()
{
   python3 -m http.server 8000 1> /var/log/example/server.log 2>/var/log/example/server_error.log &
   echo "starting!";
}

do_stop()
{
   pkill python3 # TODO. Kill by ID
   echo "stopping!"
}

# Verify the status of MATH
status() {
    echo "Checking status of MATH..."
    #Verify if the service is running
    $PGREP -f MATH > /dev/null
    VERIFIER=$?
    if [ $ZERO = $VERIFIER ]
    then
        echo "Service is running"
    else
        echo "Service is stopped"
    fi
    echo
}

case "$1" in
   start)
     do_start
     ;;
   stop)
     do_stop
     ;;
esac


exit 0