We have a lot of remote sites and if there has been and outage, sometimes dhcp doesn't come up when the server restarts and users are unable to login - it would be nice to clear up this problem automatically.

I have a script which (supposedly) tests whether dhcpd is running. The script is running as root but it will not start dhcpd. If I substitute another service (httpstkd for example) the script works as expected. Obviously, if run from the command line the service starts as expected. What am I missing?

The sites are a mixture of SLES10 sp2 and sp3.


#Defined variables
LOGDATE=$(date +"%d%m%Y")

echo "This script will check to see if the $SERVICE1 is running. If it is not running this script will start the process. This script will also log the event and email $EMAIL1,$EMAIL2."

#Check to see if log file exists already, if not create log.
if [ -e $LOG ]
then echo "Log exists"
else touch $LOG ; echo "Log file $LOG created"

#Write header to log.
echo -e "-------------------------$SERVICE1 Service Check Log $LOGDATE-------------------------">>$LOG

#Check to see if this process has already failed.
if [ -e /var/log/dhcpd_down ]
then echo $(date) "Looks like DHCP has already failed to start, $HOST will stop spamming now">>$LOG

#Check to see that dhcpd process is running.
if ps ax | grep -v grep | grep dhcpd > /dev/null 2>&1

echo -e $(date) "$SERVICE1 service is running.">>$LOG

#If service is not running try to restart and send email notification.
touch /var/log/dhcpd_down
echo -e $(date) "Sending Email Notice that $SERVICE1 is not running to $EMAIL1,$EMAIL2">>$LOG
echo -e $(date) "$SERVICE1 is not running, attempting to restart $SERVICE1 now....">>$LOG
echo -e $(date) "$HOST $SERVICE1 is not running! attempting to restart" | mail -s "$HOST $SERVICE1 is down." $EMAIL1,$EMAIL2

/etc/init.d/dhcpd start

#Import last 15 lines from /var/log/messages to help with troubleshooting.
echo -e $(date) "Importing last 15 lines from /var/log/messages to $LOG now.... \r \r From /var/log/messages:">>$LOG
tail -15 /var/log/messages>>$LOG
echo -e "End /var/log/messages">>$LOG
echo -e "\r">>$LOG

#Check to see that service started correctly and send email notification.
ps ax | grep -v grep | grep dhcpd > /dev/null 2>&1
echo -e $(date) "$SERVICE1 started successfully">>$LOG
echo -e $(date) "Sending Email Notice that $SERVICE1 restarted successfully $EMAIL1,$EMAIL2">>$LOG
echo -e $(date) "$HOST $SERVICE1 has been restarted successfully" | mail -s "$HOST $SERVICE1 restarted" $EMAIL1,$EMAIL2
rm /var/log/dhcpd_down

#If service failed to start send email notification.
echo -e $(date) "$SERVICE1 failed to restart">>$LOG
echo -e $(date) "Sending Email Notice for $SERVICE1 restart failed $EMAIL1,$EMAIL2">>$LOG
echo -e $(date) "$HOST $SERVICE1 restart failed!" | mail -s "$HOST $SERVICE1 restart failed" $EMAIL1,$EMAIL2

#Import last 15 lines from /var/log/messages to help with troubleshooting.
echo -e $(date) "Importing last 15 lines from /var/log/messages to $LOG now.... \r \r From /var/log/messages:">>$LOG
tail -15 /var/log/messages>>$LOG
echo -e "End /var/log/messages">>$LOG
echo -e "\r">>$LOG

ls -t /var/log/dhcpd_*.log | awk 'NR>5 {system("rm \"" $0 "\"")}'


#end script