Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jccartwright/559fbdd3d8ad050fe11ca45b5568153d to your computer and use it in GitHub Desktop.
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
#!/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