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/4c4e3fadd1413ea87b9315afdca792b9 to your computer and use it in GitHub Desktop.
Save jccartwright/4c4e3fadd1413ea87b9315afdca792b9 to your computer and use it in GitHub Desktop.
process_ctl, take 2
#!/usr/local/bin/bash
pid_file="/Users/jcc/pid_file"
command="ping -i 60 localhost"
###############################################################################
## user customization below this should not be required ##
###############################################################################
function start {
echo "starting..."
#$command
$command &
pid=$!
echo $pid > $pid_file
echo "process id is $pid"
}
function stop {
echo "stopping..."
kill $pid
#check to see if process died immediately
updateProcessStatus
#otherwise wait for it to die
while [ ! -z "$pid" ]
do
echo "waiting for process $pid to stop..."
sleep 2
updateProcessStatus
done
rm $pid_file
pid=""
echo "process $pid stopped"
}
# update the global variable to indicate status
function updateProcessStatus {
if [ ! -z "$pid" ]
then
#echo "checking for process $pid..."
#BSD/POSIX doesn't support --no-headers
count=$(ps -o %cpu= -p $pid|wc -l)
#count=$(ps -lfhp $pid|wc -l)
if [ "$count" -eq 0 ]
then
pid=""
fi
fi
}
function checkForAbandonedPidFile {
if [ ! -z "$pid" ]
then
#BSD/POSIX doesn't support --no-headers
count=$(ps -o %cpu= -p $pid|wc -l)
#count=$(ps -lfhp $pid|wc -l)
if [ "$count" -eq 0 ]
then
echo "WARNING: detected abandoned PID file. removing it now..."
pid=""
rm $pid_file
fi
fi
}
function checkForBlankPidFile {
if [ -e "$pid_file" -a -z $"pid" ]
then
echo "WARNING: PID file found but with blank PID. removing it now..."
rm $pid_file
fi
}
function initPID {
if [ ! -e "$pid_file" ]
then
# a little risky, but w/o a PID, assume process is not running
pid=""
else
pid=$(cat "$pid_file")
echo "process ID is $pid"
fi
}
initPID
checkForBlankPidFile
checkForAbandonedPidFile
case "$1" in
'start')
if [ -z "$pid" ]
then
start
else
echo "process $pid already running"
fi
;;
'stop')
if [ ! -z "$pid" ]
then
stop
else
echo "process is not running"
fi
;;
'status')
if [ ! -z "$pid" ]
then
echo "process $pid 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