dhcpd won't start from script

We have a lot of remote sites and if there has been and outage, sometimes dhcp doesn’t come up in time 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.

[size=2]if ps ax | grep -v grep | grep dhcpd > /dev/null 2>&1

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

    /etc/init.d/dhcpd start[/size]

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 eun from the command line the service starts as expected. What am I missing?

Welcome here (I see this is your first post).

As many first posters, you seem to think that we can guess what level of openSUSE you are running. This is not the case. Thus please, allways mention this. I this case a desktop seems not involved, but when there is any desktop aspect involved in an eventual next question you have, do not forget to tell which one you use.

Also (and this is less obvious for first users of the forums software here), we like to have computer output posted between CODE] tags for several reasons. Please see: http://forums.opensuse.org/english/information-new-users/advanced-how-faq-read-only/451526-posting-code-tags-guide.html

While it might not be of great interest for the exact problem, I want to point to the fact that the statements you show are just a bunch of statements and no script. A script starts with a “shebang” like:

#!/bin/bash

to tell the system what sort of script it is. And in the case of posting the script here, it will tell us what sort of script it is.

Apologies.

It’s SLES 10.2 in the particular instance. We have a mixture of 10.2 and 10.3

#!/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=kat888

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

On 02/21/2012 11:16 PM, kat888 wrote:
> SLES 10.2 in the particular instance. We have a mixture of 10.2
> and 10.3

-=WELCOME=- new poster!

you are welcome to seek advice here, but BE ADVISED that these are the
openSUSE forums and many of the answers might be from folks who have
never run SLES (or maybe never even heard of it before) and
you are likely much better off if you seek assistance from the
Attachmate/SUSE forums (where the SUSE Linux Enterprise gurus hang out),
at: http://forums.suse.com/

your ID/Pass here works there also…


DD
What does DistroWatch write about YOU?: http://tinyurl.com/SUSEonDW

On 2012-02-21 23:16, kat888 wrote:
>
> Apologies.
>
> It’s SLES 10.2 in the particular instance. We have a mixture of 10.2
> and 10.3

Mmmm. These are the openSUSE forums, many of us have not ever used SLES.
They use different forums.

Besides, there is no such thing as SLES 10.2, only SLES 10. You might mean
you are using service pack 2.

Anyway:

>
>
> Code:
> --------------------

> /etc/init.d/dhcpd start

> --------------------

That line should print things, so you should capture them. Some services
refuse starting (perhaps you should use “restart” instead) if the service
crashed because there is a PID file somewhere.


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

Right then, I’m off. T’ra.