Bash store command PID in variable and kill process

12,480

$$ is the current process which means the script is killing itself. You can get the process ID of the last process you started in the background, with $! but it appears you're not actually starting one of those.

With your code segment:

udhcpc -i eth0
pid=$$

the pid= line will only be executed when udhcpc exits (or daemonises itself, in which case neither $$ nor $! will work anyway), so there's zero point in trying to kill of the process.

To run it in the background and store its process ID, so you can continue to run in the parent, you could use something like:

udhcpc -f -i eth0 &
pid=$!

and you're using -f to run in foreground in that case, since you're taking over the normal job control.

Or, alternatively, since udhcpc can create its own PID file, you can use something like:

udhcpc -i eth0 -p /tmp/udhcpc.eth0.pid
pid=$(cat /tmp/udhcpc.eth0.pid)
Share:
12,480
ogs
Author by

ogs

Updated on June 05, 2022

Comments

  • ogs
    ogs almost 2 years

    I would like to use a shell script in order to establish ethernet connection.

    I am using a function implemented as :

    function connec()
    {
        ip link set eth0 up
        sleep 5
        udhcpc -i eth0
        pid=$$
        echo $pid
        ps 
        kill -9 $pid
    }
    

    However, the script returns :

    743
    743 root      2704 S    {script.sh} /bin/bash ./script.sh connect
    767 root      2200 S    udhcpc -i eth0
    Killed
    

    I don't succeed in store 767 rather than 743. I also tried by using $! but in that specific case "echo $pid" returns 0.

    • anishsane
      anishsane about 9 years
      Under above situation, I don't think you need to run udhcpc in background & then kill using pid. If you want to have some timeout for which you want to allow the process to run, use timeout command itself. If the command udhcpc runs in foreground, next command will be run only after that command completes. So you don't have to kill that command using pid.
  • ogs
    ogs about 9 years
    Than paxdiablo for your answer. Use "udhcpc -i eth0 &" works but I note a weird behavior. Indeed, echo $pid returns the good process number and the udhcpc command is executed. I added a sleep between pid=$! in order to let enough time to udhpc to be entirely executed but the kill -9 $pid returns kill: (1813) - No such process because the process number has been changed ? I don't know why !!? The problem is that at each occurrence of connect() a new udhcpc process is created .. I think killall udhcpc is a solution but is really a proper one ?
  • paxdiablo
    paxdiablo about 9 years
    @SnP, it's probably daemonising itself (starting another process to do the grunt-work, and exiting the one you started). That would explain the missing process even though it's still running. The PID you got was the immediate child, the process left running is the grandchild. Either add -f to stop that or, probably better, use the PIDfile solution I provided.