Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send custom signal to bash daemon process?

I have simple bash daemon running (with root privileges ) in background which suppose to do action1 or/and action2 when notified.

How do I notify it/send some kind of signal on which it will react?

I've tried scenarios with checking file change every 1 sec or more often, but that's kind of less-desirable solution.

like image 542
greenV Avatar asked Mar 22 '15 21:03

greenV


1 Answers

You can send signals to a process using the kill command. There is a range of standard signals as well as two user defined signals, which you can let your script handle whichever way you prefer. Here is how this could look in a script

#!/bin/bash

handler(){
    echo "Handler was called"
}

trap handler USR1

while sleep 1
do
    date
done

To send a signal to the script you first need to find the pid of the script and then use the kill command. It could look like this kill -USR1 24962.

like image 196
kasperd Avatar answered Oct 14 '22 07:10

kasperd