megadesign
September 19th, 2007, 05:32 AM
Hi!
I wrote a small sh script for checking if particular process is running.
The script need to be started as "service" (./script &). If you want to start it as soon as Ubuntu is loaded, add line "/path/to/script/script.sh &" without quotes to /etc/rc.local.
When script is running, it periodically check if defined process name is up and running.
When checked process isn't found (crashed!), the script will send some warning e-mail to the defined address and will try to start that process again (check permissions to run process). This warning e-mail is send only once until process is again started.
If starting of the checked process is successful, e-mail with this info is send.
Save code bellow to some file and enable permissions for executing of that file.
#!/bin/sh
MAILSTOP=0 # switcher for mail sending
CHECKINGPERIOD=20 # in sec
MYEMAIL="me@myemail.com" # where to send info
while [ 1=1 ];
do
if [ ! "$(pidof httpd)" ]
then
if [ "$MAILSTOP" = "0" ]; then
echo "WARNING! Apache crashed!" | mail -s "Apache crash." "$MYEMAIL"
MAILSTOP=1
fi
/www/bin/apachectl start
else
if [ "$MAILSTOP" = "1" ]; then
echo "Apache was successfully restarted." | mail -s "Apache restarted." "$MYEMAIL"
MAILSTOP=0
fi
fi
sleep $CHECKINGPERIOD
done
I'm using it for checking Apache web server every 20 seconds, so this process name (httpd) is in code above. For me, it send sms-email to my mobile phone. This all can be changed for your needs. I'm sure, it can do a big range of another things, but this is basic script, which fulfil my needs.
Requirements:
In Ubuntu 7.04 it requires installation of mail command, which is in package mailx. For example:
apt-get install mailx
Enjoy!
I wrote a small sh script for checking if particular process is running.
The script need to be started as "service" (./script &). If you want to start it as soon as Ubuntu is loaded, add line "/path/to/script/script.sh &" without quotes to /etc/rc.local.
When script is running, it periodically check if defined process name is up and running.
When checked process isn't found (crashed!), the script will send some warning e-mail to the defined address and will try to start that process again (check permissions to run process). This warning e-mail is send only once until process is again started.
If starting of the checked process is successful, e-mail with this info is send.
Save code bellow to some file and enable permissions for executing of that file.
#!/bin/sh
MAILSTOP=0 # switcher for mail sending
CHECKINGPERIOD=20 # in sec
MYEMAIL="me@myemail.com" # where to send info
while [ 1=1 ];
do
if [ ! "$(pidof httpd)" ]
then
if [ "$MAILSTOP" = "0" ]; then
echo "WARNING! Apache crashed!" | mail -s "Apache crash." "$MYEMAIL"
MAILSTOP=1
fi
/www/bin/apachectl start
else
if [ "$MAILSTOP" = "1" ]; then
echo "Apache was successfully restarted." | mail -s "Apache restarted." "$MYEMAIL"
MAILSTOP=0
fi
fi
sleep $CHECKINGPERIOD
done
I'm using it for checking Apache web server every 20 seconds, so this process name (httpd) is in code above. For me, it send sms-email to my mobile phone. This all can be changed for your needs. I'm sure, it can do a big range of another things, but this is basic script, which fulfil my needs.
Requirements:
In Ubuntu 7.04 it requires installation of mail command, which is in package mailx. For example:
apt-get install mailx
Enjoy!