3

The following command works perfectly to start DHCP:

# dhcpcd -p eth0

DHCP cannot be stopped once it is started. The following command does not work:

# dhcpcd -k eth0

It returns the following:

dhcpcd[4700]: sending signal 1 to pid 4086
dhcpcd[4700]: dhcpcd not running
dhcpcd[4700]: kill: Bad file number

Could anyone offer a tip on how to kill DHCP?

6
  • 1
    Is the pid 4086 a constant, or does it change?
    – Grimoire
    Apr 17 '16 at 14:27
  • @DeathMaskSalesman No. I have just rebooted the device and run the command again. The PID is 4043 this time.
    – Hong
    Apr 17 '16 at 14:32
  • 1
    If you have to include this in a script, this snippet may help you (it has to be ran as root): var=$(ps | grep dhcp). Then, in a new line, do kill $(echo -n $var | cut -d " " -f 0), and it should kill the process altogether.
    – Grimoire
    Apr 17 '16 at 14:36
  • @DeathMaskSalesman Thanks a lot. It will do though it would be ideal to have one command to to this. I can use PS to get the PID, then kill it. I have tested it, and it works. Could you turn your comment into an answer that I can accept?
    – Hong
    Apr 17 '16 at 14:51
  • Doesn't su -c 'killall dhcpcd' work?
    – Firelord
    Apr 17 '16 at 15:19
7

A way to dynamically get the PID of the dhcp process, and kill it altogether, would be to run:

su
var=$(ps | grep dhcp)
kill $(echo -n $var | cut -d " " -f 0)

Specifically:

  • line 1 asks for root permissions;
  • line 2 assigns the output of ps (which lists the active processes), filtered by grep with the keyword dhcp, to the variable var;
  • line 3 calls kill to terminate the value contained in the first field (-f 0) of the variable var, by telling cut to separate the fields with a whitespace (-d " "). Not coincidentally, the mentioned value corresponds to the PID of the process dhcpcd.
3
  • 3
    That would kill any process with "dhcp" in its name. Even killall dhcpcd would be a better choice. Apr 17 '16 at 20:49
  • @SimonRichter You are in error: since I make use of cut, only the first PID is a candidate for killing. You are encouraged to experiment with it yourself, if you don't believe the correctness of the answer.
    – Grimoire
    Apr 17 '16 at 21:03
  • 2
    "any" means "any of them", not "all of them". Specifically, it kills the one with the smallest PID, so e.g. if I have two Ethernet ports, and run dhcpd on one, and dhcpcd on the other, then the dhcpd is likely to be started first because it has higher priority during boot, and your command would kill that instead. Also, if dhcpcd has a high PID, the chance is high that you match the grep dhcp command itself, which has terminated by the time kill is called, so it would do nothing in this case. Apr 17 '16 at 21:16
1

This could be done with awk as well. The commands would be;

su

kill -9 $(ps -e | grep dhcp | awk '{ print $1 }')

Details

su to jump to root shell, kill -9 to force kill ps -e lists all the processes in details, we pipe it through grep dhcp which only outputs anything from ps -e output to dhcp, then we pipe it through awk, the print $1 only prints the contents of the first field, which is the process id of dhcp. And kill will kill the process matching that process id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.