如何在bash中制作回调示例运行?

How to make the callback example run in bash?

我读过关于 bash 回调的讨论,jlliagre post 一个了不起的例子。
callback example posted by jlliagre
在这里背诵要点。
1.Create 回调示例和 运行 它在终端上 ./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另一个终端,发送USR1信号给shell进程

$ pkill -USR1 callback-example

作者说:发送的每个信号都应触发第一个终端中显示如下行:

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

我发现我的 bash 中从未发生过这种情况,如何修复它以及 bash 中的 运行?

您的脚本在进程列表中显示为 /bin/bash ./callback-example 而不仅仅是 ./callback-example

将选项 -f 添加到您的 pkill 命令。

来自man pkill

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

kill -USR1 `ps aux|grep callbac[k] |awk '{print }'`

pkill -USR1  -f  callback-example