# dhcpd won't start from script

**URL:** <https://forums.suse.com/t/dhcpd-wont-start-from-script/22253>\
**Category:** SLES Configure-Administer\
**Created:** [February 22, 2012, 11:48pm UTC](https://forums.suse.com/t/dhcpd-wont-start-from-script/22253 "2012-02-22T23:48:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kat888](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kat888](https://forums.suse.com/u/kat888)\
**Post date:** [February 22, 2012, 11:48pm UTC](https://forums.suse.com/t/dhcpd-wont-start-from-script/22253/1 "2012-02-22T23:48:33Z")

</div>

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.

* * *

#!/bin/bash

# 

#Defined variables  
LOGDATE=$(date +“%d%m%Y”)  
LOG=“/var/log/dhcpd\_daemon\_check\_$LOGDATE.log”  
SERVICE1=‘DHCP Daemon’  
HOST=$(hostname)  
EMAIL1=root  
[EMAIL2=me@overthere.net](mailto:EMAIL2=me@overthere.net)

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”  
fi

#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  
exit  
fi

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

then  
echo -e $(date) “$SERVICE1 service is running.”\>\>$LOG  
elif

#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  
then

#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  
else

#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 “"”)}’

fi

#end script

---

<div class="post-metadata">

**Author:** ![Jens-U](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@Jens-U](https://forums.suse.com/u/Jens-U)\
**Post date:** [February 23, 2012, 5:21pm UTC](https://forums.suse.com/t/dhcpd-wont-start-from-script/22253/2 "2012-02-23T17:21:27Z")

</div>

kat888,

what’s both the script output and dhcpd syslog messages in case starting from script fails?

Is that a c&p of the script? Your rather longish “if” condition around line 35 makes reading it rather troublesome… what’s the intention of that code block?

Regards,  
Jens

---

<div class="post-metadata">

**Author:** ![kat888](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kat888](https://forums.suse.com/u/kat888)\
**Post date:** [March 9, 2012, 7:12am UTC](https://forums.suse.com/t/dhcpd-wont-start-from-script/22253/3 "2012-03-09T07:12:51Z")

</div>

Thanks Jens, apologies for taking so long to respond to you – been having fun with other more pressing problems.

I’ve managed to get the script working in any case, by replacing the following line (35)

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

cheers

Kat
