Linux, Send USR1 signal to pid

In Linux you can send signals to a process id to trigger actions for the program. Useful scenario for this is to renew an IP address on a device that uses udhcpc. You should be able to change udhcpc for other programs, you’ll just need to read the help for that specific program.

In the udhcpc help it says

Signals:
         USR1    Renew lease
         USR2    Release lease

But how do we send those signals to udhcpc? Answer, use the kill command.

kill: kill [-s sigspec | -n signum | -sigspec] pid | jobspec … or kill -l [sigspec]
     Send a signal to a job.

Send the processes identified by PID or JOBSPEC the signal named by SIGSPEC or SIGNUM.  If neither SIGSPEC nor SIGNUM is present, then SIGTERM is assumed. 

Options:   
-s sig    SIG is a signal name   
-n sig    SIG is a signal number   
-l        list the signal names; if arguments follow `-l' they are             
          assumed to be signal numbers for which names should be listed   
-L        synonym for -l 

Kill is a shell builtin for two reasons: it allows job IDs to be used instead of process IDs, and allows processes to be killed if the limit on processes that you can create is reached.
Exit Status:
Returns success unless an invalid option is given or an error occurs. 

We see from above that we can pass a signal name in using the -s option.

So to send USR1 signal to udhcp we do the following

kill -s USR1 pid_of_udhcpc

Replace pid_of_udhcpc with the actual pid or use the following command to find the pid

kill -s USR1 $(pgrep udhcpc)

“pgrep udhcpc” prints the pid of the searched for process.

Helpful links
https://www.thegeekstuff.com/2011/02/send-signal-to-process/
https://www.linux.org/threads/kill-signals-and-commands-revised.11625/

Leave a Reply

Your email address will not be published. Required fields are marked *