Skip to content

Instantly share code, notes, and snippets.

@alessandro-fazzi
Created July 9, 2011 16:33
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 alessandro-fazzi/1073720 to your computer and use it in GitHub Desktop.
Save alessandro-fazzi/1073720 to your computer and use it in GitHub Desktop.
Watch number of instances of a process and restart it if greater than x
#!/bin/bash
initscript="/etc/init.d/myprocess"
mail=$(which mail)
destinatario="mail@example.com"
oggetto='Mail subject'
data=$(date)
limite=190 #must be an integer
process="" #if the process to restart differ from the name of the initscript, then set it here
#if $process isn't set than we set with the same name of the initscript
#+ e.g.: /etc/init.d/apache2 -> process will be "apache2"
[[ $process ]] || process=`basename $initscript`
if [ `ps -e -o comm= | grep $process | uniq -c | sed 's/[^0-9]//g'` -gt $limite ]; then
logger [process control] restarting `basename $initscript` due to $process processes saturation
${initscript} restart
pkill imapd
echo "Come da oggetto... Sto lavorando per voi. ${data}" | ${mail} -s "${oggetto}" ${destinatario}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment