Skip to content

Instantly share code, notes, and snippets.

@melnik13 melnik13/lalala.sh
Last active Aug 29, 2015

Embed
What would you like to do?
A quick and dirty cronjob to reset some service whel LA raises way too high
#!/bin/bash
#
# Just put it to your crontab:
#
# * * * * * root /root/bin/lalala.sh --la1 50 --la5 20 --laf 10 --service httpd --kill --process httpd
#
# It will kill all httpd processes and restart httpd service when LA1 becomes greater or equal to 50, LA5 - to 20 and LA15 - to 10
#
PATH=/bin:/sbin:/usr/bin
OPTS=$(getopt -n $(basename ${0}) -o 1:5:f:s:kp: -l la1:,la5:,laf:,service:,kill,process: -- "${@}")
if [ "${?}" -ne 0 ]; then
echo "Can't get options" >&2
exit 1
fi
eval set -- "${OPTS}"
while :; do
case "${1}" in
-1|--la1)
THR1="${2}"; shift 2;;
-5|--la5)
THR5="${2}"; shift 2;;
-f|--laf)
THR15="${2}"; shift 2;;
-s|--service)
SERVICE="${2}"; shift 2;;
-k|--kill)
KILL="1"; shift 1;;
-p|--process)
PROCESS="${2}"; shift 2;;
--)
shift; break;;
*)
echo "Can't recognize the option: ${1}"; exit 1;;
esac
done
if [ -z "${SERVICE}" ]; then
echo "Can't understand what service shall I restart" >&2
exit 1
fi
if [ "${KILL}" ] && [ -z "${PROCESS}" ]; then
echo "Can't understand what process shall I kill" >&2
exit 1
fi
LA=$( uptime | sed -r 's/^.*load average: ([0-9]+)(\.[0-9]+)?, ([0-9]+)(\.[0-9]+)?, ([0-9]+)(\.[0-9]+)?$/\1\t\3\t\5/g' )
LA1=$( echo "${LA}" | cut -f1)
LA5=$( echo "${LA}" | cut -f2)
LA15=$(echo "${LA}" | cut -f3)
if
[ -n "${THR1}" ] && [ -n "${LA1}" ] && [ "${LA1}" -ge "${THR1}" ] &&
[ -n "${THR5}" ] && [ -n "${LA5}" ] && [ "${LA5}" -ge "${THR5}" ] &&
[ -n "${THR15}" ] && [ -n "${LA15}" ] && [ "${LA15}" -ge "${THR15}" ];
then
if [ "${KILL}" ]; then
while PIDS=$(pidof ${PROCESS}); do
echo "Slaying ${PIDS}..."
kill -9 ${PIDS}
sleep 1
done
sleep 10
else
service "${SERVICE}" restart
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.