LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Enabling simple init script to restart compiled dhcpd (https://www.linuxquestions.org/questions/linux-newbie-8/enabling-simple-init-script-to-restart-compiled-dhcpd-4175418746/)

w1cx3d 07-26-2012 04:51 AM

Enabling simple init script to restart compiled dhcpd
 
Hi! This is my first time posting here!
I recently compiled the isc dhcp server and wrote a simple
init script to enable it to automatically start after booting.
The start and stop parts of the script are working pretty well,
much different to the way the restart part behaves.

Any help for figuring out why this isn't working as it should
would be greatly appreciated:

The script:
Code:

student@ubuntu12:~$ cat /etc/init.d/dhcpd
#!/bin/bash

case $1 in

start) /opt/dhcpd/sbin/dhcpd -cf /opt/dhcpd/etc/dhcpd.conf \
        -lf /opt/dhcpd/etc/dhcpd.leases ;;

stop) pkill dhcpd ;;

restart) echo "turned off"
        $0 stop
        echo "this line got executed"
        sleep 2
        $0 start
        echo "turned on" ;;

*) echo "sintax: $0 {start|stop|restart}"
echo "        service dhcpd {start|stop|restart}"

esac
stud@ubuntu12:~$

Command output:
Code:

student@ubuntu12:~$ pgrep -l dhcpd
student@ubuntu12:~$ sudo service dhcpd start
Internet Systems Consortium DHCP Server 4.2.4-P1
Copyright 2004-2012 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Wrote 0 deleted host decls to leases file.
Wrote 0 new dynamic host decls to leases file.
Wrote 5 leases to leases file.
Listening on LPF/eth0/00:d0:de:31:ac:9f/10.20.30.0/24
Sending on  LPF/eth0/00:d0:de:31:ac:9f/10.20.30.0/24
Sending on  Socket/fallback/fallback-net
student@ubuntu12:~$ pgrep -l dhcpd
2322 dhcpd
student@ubuntu12:~$ sudo service dhcpd restart
turned off
student@ubuntu12:~$ pgrep -l dhcpd
student@ubuntu12:~$


chrism01 07-26-2012 08:16 PM

1. try amending top of script
Code:

#!/bin/bash
set -xv    # this line shows what parser is doing

2. why compile dhcpd instead of just getting it from your distro's repos?
Much easier...

w1cx3d 07-27-2012 04:18 AM

Hi! Thanks for your reply. This week we're having a school project in which
we're supposed to compile dhcpd and make it start normally after booting.


Sorry for the long follow-up..

I appended your line to my script and it looked like it was getting
interrupted right after executing the "$0 stop" line of the restart case:

Code:

student@ubuntu12:~$ sudo service dhcpd restart

< .. output of the script in it's entirety .. >

+ case $1 in
+ echo 'turned off'
turned off
+ /etc/init.d/dhcpd stop

< .. output of the script in it's entirety .. >

+ case $1 in
+ pkill dhcpd
student@ubuntu12:~$


I'did a little bit of tweaking to the code by adding "ps -ef | grep dhcpd"
to the restart case:
Code:

restart) echo "turned off"
        ps -ef | grep dhcpd
        $0 stop
        echo "this line got executed"
        sleep 2
        $0 start
        echo "turned on" ;;

Here's the output I got after adding that line:
Code:

student@ubuntu12:~$ sudo service dhcpd restart

< .. output of the script in it's entirety .. >

+ case $1 in
+ echo 'turned off'
turned off
+ ps -ef
+ grep dhcpd
root      2172    1  0 11:27 ?        00:00:00 /opt/dhcpd/sbin/dhcpd -cf
                      /opt/dhcpd/etc/dhcpd.conf -lf /opt/dhcpd/etc/dhcpd.leases
root      2174  2013  0 11:28 pts/2    00:00:00 sudo service dhcpd restart
root      2175  2174  0 11:28 pts/2    00:00:00 /bin/bash /etc/init.d/dhcpd restart
root      2179  2175  0 11:28 pts/2    00:00:00 grep dhcpd
+ /etc/init.d/dhcpd stop

< .. output of the script in it's entirety .. >

+ case $1 in
+ pkill dhcpd
student@ubuntu12:~$

So it turned out the script was sending itself the kill signal all along.
I changed the script's name to dhcpsrv, and sure enough it is now working
properly.


Again, many thanks for the input,
Mike

chrism01 07-29-2012 06:10 PM

No worries. :)

Here's some useful links:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/


All times are GMT -5. The time now is 04:14 PM.