A Bash Process Controller to Start, Stop and Restart an Daemon Program


In Linux, we have service process controller which can be used to start, stop and restart a particular registered service. For example:

1
2
3
$ sudo service mysql stop
$ sudo service mysql start
$ sudo service mysql restart
$ sudo service mysql stop
$ sudo service mysql start
$ sudo service mysql restart

We can use BASH script to customize a process controller, where we can pass the third paramter any long-running process e.g. yes which keeps printing the “y” to the console.

We use “nohup” to start the daemon program and put it in background. Output the process ID to $PID_FILE_PATH so that we can control it later with stop. The “restart” is an alias of “stop” and “start”.

If the PID_FILE_PATH is found, we search the process in memory using “ps” command, and exit the BASH if the process is found running.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
 
# Function: to start, stop and restart the application.
# Usage: $0 start|stop|restart application
 
if [[ "$#" -lt 2 ]]; then
    echo "Usage: $0 start|stop|restart command"
    echo "$# parameters given."
    exit 1
fi
 
PID_FILE_PATH="runner.pid"
 
case "${1}" in
    start)
        # Already running
        if [ -f ${PID_FILE_PATH} ]; then
            pid=$(cat ${PID_FILE_PATH})
            if $(ps -p ${pid} > /dev/null); then
                echo "Already running [PID: ${pid}], you can stop it and retry."
                exit 1
            fi
        fi
 
        nohup "$2" 2>&1 \
            & echo $! > ${PID_FILE_PATH}
 
        if [ $? -eq 0 ]; then
            echo "Succeeded to start $2."
        else
            echo "Failed to start $2."
            exit 1
        fi
    ;;
    stop)
        kill $(cat ${PID_FILE_PATH})
 
        if [ $? -eq 0 ]; then
            rm ${PID_FILE_PATH}
            echo "Succeeded to stop $2."
        else
            echo "Failed to stop $2."
            exit 1
        fi
    ;;
    restart)
        ${0} stop "$2" && sleep 1 && ${0} start "$2"
    ;;
esac
#!/bin/bash

# Function: to start, stop and restart the application.
# Usage: $0 start|stop|restart application

if [[ "$#" -lt 2 ]]; then
    echo "Usage: $0 start|stop|restart command"
    echo "$# parameters given."
    exit 1
fi

PID_FILE_PATH="runner.pid"

case "${1}" in
    start)
        # Already running
        if [ -f ${PID_FILE_PATH} ]; then
            pid=$(cat ${PID_FILE_PATH})
            if $(ps -p ${pid} > /dev/null); then
                echo "Already running [PID: ${pid}], you can stop it and retry."
                exit 1
            fi
        fi

        nohup "$2" 2>&1 \
            & echo $! > ${PID_FILE_PATH}

        if [ $? -eq 0 ]; then
            echo "Succeeded to start $2."
        else
            echo "Failed to start $2."
            exit 1
        fi
    ;;
    stop)
        kill $(cat ${PID_FILE_PATH})

        if [ $? -eq 0 ]; then
            rm ${PID_FILE_PATH}
            echo "Succeeded to stop $2."
        else
            echo "Failed to stop $2."
            exit 1
        fi
    ;;
    restart)
        ${0} stop "$2" && sleep 1 && ${0} start "$2"
    ;;
esac

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ./runner.sh stop yes
cat: runner.pid: No such file or directory
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
Failed to stop yes.
$ ./runner.sh start yes
Succeeded to start yes.
nohup: appending output to 'nohup.out'
$ ./runner.sh restart yes
Succeeded to stop yes.
Succeeded to start yes.
nohup: appending output to 'nohup.out'
$./runner.sh stop yes
Succeeded to stop yes
.
$ ./runner.sh stop yes
cat: runner.pid: No such file or directory
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
Failed to stop yes.
$ ./runner.sh start yes
Succeeded to start yes.
nohup: appending output to 'nohup.out'
$ ./runner.sh restart yes
Succeeded to stop yes.
Succeeded to start yes.
nohup: appending output to 'nohup.out'
$./runner.sh stop yes
Succeeded to stop yes
.

BASH Programming/Shell

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
452 words
Last Post: Teaching Kids Programming - Breadth First Search Algorithm to Determine Sum Binary Tree
Next Post: Teaching Kids Programming - Depth First Search Algorithm to Determine Sum Binary Tree

The Permanent URL is: A Bash Process Controller to Start, Stop and Restart an Daemon Program

Leave a Reply