How to make the callback example run in bash?

445 views Asked by At

I had read a discussion on bash's callback,jlliagre post an amazing example.
callback example posted by jlliagre
Recite the main points here.
1.Create callback-example and run it on terminal with ./callback-example.

#!/bin/bash
myCallback() {
    echo "I've been called at $(date +%Y%m%dT%H%M%S)"
}
# Set the handler
trap myCallback SIGUSR1
# Main loop. Does nothing useful, essentially waits
while true; do
    read foo
done

2.On another terminal, send the USR1 signal to the shell process.

$ pkill -USR1 callback-example

The author say :Each signal sent should trigger the display of lines like these ones in the first terminal:

I've been called at 20180925T003515
I've been called at 20180925T003517

I found that it never happen in my bash,how to fix it and run in bash?

2

There are 2 answers

0
Cyrus On BEST ANSWER

Your script appears in the process list as /bin/bash ./callback-example and not only as ./callback-example.

Add option -f to your pkill command.

From man pkill:

-f: The pattern is normally only matched against the process name. When -f is set, the full command line is used.

0
AudioBubble On
kill -USR1 `ps aux|grep callbac[k] |awk '{print $2}'`

OR

pkill -USR1  -f  callback-example