In Linux, we have service process controller which can be used to start, stop and restart a particular registered service. For example:
$ 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.
#!/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:
$ ./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
- Alarming on High Disk Usage using BASH, AWK, Crontab Job
- Compute GCD in Bash with Input Validation
- Why a Leading Space in Linux Shell Commands Can Matter?
- How to Get HTTP Response Code using cURL Command?
- Three Interesting/Fun BASH Commands
- One Interesting Linux Command (Steam Locomotive in BASH)
- Simple Bash Function to Repeat a Command N times with Retries
- How to Extract a Domain Name from a Full URL in BASH?
- BASH Function to Compute the Greatest Common Divisor and Least Common Multiples
- BASH Function to Get Memory Information via AWK
- BASH Function to Escape Parameters Argument
- BASH Function to Check if sudo Available
- Full Permutation Algorithm Implementation in BASH
- BASH Function to Install Docker
- A Simple BASH Function/Script to Run a Command in Background
- BASH Script to Compute the Average Ping to a Domain
- A Bash Process Controller to Start, Stop and Restart an Daemon Program
- Linux BASH shell - Echo This if you get angry
- Bash SHELL, Chess Board Printing
–EOF (The Ultimate Computing & Technology Blog) —
435 wordsLast 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