Created
August 12, 2015 22:07
-
-
Save chrisroberts/8f4aacc9a7d38981ad9a to your computer and use it in GitHub Desktop.
proc killer
This file contains hidden or 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 | |
if [[ "$#" -gt 2 ]]; then | |
echo "ERROR: Only two arguments are allowed!" | |
exit -1 | |
elif [[ "$#" -lt 1 ]]; then | |
echo "${0}: Kill processes running past limit (default is one hour)" | |
echo "Usage: " | |
echo "" | |
echo " ${0} PROC_PATTERN [MAX_AGE_IN_SECONDS]" | |
exit -1 | |
fi | |
proc_pattern=${1} | |
age_limit=${2:-3600} | |
processes="$(pgrep -f ${proc_pattern})" | |
for i in $processes | |
do | |
if [[ -n "${i}" ]]; then | |
proc_life=$(echo "$(date +%s) - $(stat -c %X /proc/${i})" | bc) | |
if [[ "${proc_life}" -ge "${age_limit}" ]]; then | |
if [[ "${TESTING}" = "true" ]]; then | |
echo "[TEST]: Process to kill: ${i} - Process run time: ${proc_life} Max run time: ${age_limit}" | |
else | |
kill -s 9 "${i}" | |
fi | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment