Skip to content

Instantly share code, notes, and snippets.

@phackwer
Created February 15, 2017 18:07
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 phackwer/fc535a4303903aaef1f62ca5f260905b to your computer and use it in GitHub Desktop.
Save phackwer/fc535a4303903aaef1f62ca5f260905b to your computer and use it in GitHub Desktop.
Kill old processes
#!/bin/bash
#######################################################################################
# Author Pablo Sanchez
# RAID Bug Killer - kills $COMMAND processes older then $SECONDS
#######################################################################################
COMMAND='php'
SECONDS=900 # 15 minutes
(ps --noheaders -o pid,etime -C $COMMAND) | while read -r PID TIME; do
SavedIFS="$IFS"
IFS="-:."
Time=($TIME)
if [ "${Time[3]}" != "" ]; then
S=$((${Time[0]}*86400 + ${Time[1]}*3600 + ${Time[2]#0}*60 + ${Time[3]#0}))
elif [ "${Time[2]}" != "" ]; then
S=$((${Time[0]}*3600 + ${Time[1]#0}*60 + ${Time[2]#0}))
elif [ "${Time[1]}" != "" ]; then
S=$((${Time[0]#0}*60 + ${Time[1]#0}))
else
S=${Time[0]#0}
fi
if test $S -gt $SECONDS; then
kill -9 $PID
fi
IFS="$SavedIFS"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment