Created
February 15, 2017 18:07
-
-
Save phackwer/fc535a4303903aaef1f62ca5f260905b to your computer and use it in GitHub Desktop.
Kill old processes
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 | |
####################################################################################### | |
# 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