Created
May 24, 2019 15:27
-
-
Save jccartwright/559fbdd3d8ad050fe11ca45b5568153d to your computer and use it in GitHub Desktop.
bash script to stop/start/status a process. includes waiting for process to stop
This file contains 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 | |
pid_file="/path/to/pidfile" | |
command="./t2.sh &" | |
function start { | |
echo "starting..." | |
$command | |
pid=$! | |
echo $pid > $pid_file | |
echo "process id is $pid" | |
} | |
function stop { | |
echo "stopping..." | |
pid=$(cat "$pid_file") | |
kill "$pid" | |
while [ "$(isRunning)" == "true" ] | |
do | |
echo "waiting for process $pid to stop..." | |
sleep 2 | |
done | |
rm $pid_file | |
echo "process $pid stopped" | |
} | |
function isRunning { | |
if [ ! -e "$pid_file" ] | |
then | |
echo "false" | |
else | |
pid=$(cat "$pid_file") | |
count=$(ps -lfhp $pid|wc -l) | |
if [ "$count" == 1 ] | |
then | |
echo "true" | |
else | |
echo "false" | |
fi | |
fi | |
} | |
case "$1" in | |
'start') | |
if [ "$(isRunning)" == "false" ] | |
then | |
start | |
else | |
echo "process already running" | |
fi | |
;; | |
'stop') | |
if [ ! -e "$pid_file" ] | |
then | |
echo "no PID file found, exiting..." | |
exit 1 | |
fi | |
if [ "$(isRunning)" == "true" ] | |
then | |
stop | |
else | |
echo "$(isRunning)" | |
echo "process is not running" | |
fi | |
;; | |
'status') | |
if [ "$(isRunning)" == "true" ] | |
then | |
echo "process is running" | |
else | |
echo "process is not running" | |
fi | |
;; | |
*) | |
echo "Usage: logstash_apache_logs { start | stop | status }" | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment