Guard
Guard

Reputation: 6955

Sending kill -s USR1 from bash script stops the process while doing the same from terminal does not

There's a nodejs script called mimosa (https://github.com/dbashford/mimosa)

Nodejs uses USR1 to switch the running process to debug mode

Here's how I do it manually

$ cd myproj
$ mimosa watch -s # this runs node /path/to/mimosa watch -s
22:16:03 - Watching /Users/admin/Work/test-mimosa/assets
... # some more output
# check the pid from a different terminal
$ ps aux | grep mimosa
admin          79284   0.7  0.8  3153812 129272 s006  S+   10:16PM   0:03.57 node /opt/local/bin/mimosa watch -s
# send debug signal from the 2nd terminal
kill -s USR1 79284
# nodejs output in the 1st terminal
Hit SIGUSR1 - starting debugger agent.
debugger listening on port 5858

The same works if I run mimosa as a background process (mimosa watch -s &)

Now I need to automate the process: run mimosa, get its pid, send USR1, wait for user's SIGTERM, kill mimosa:

mimosa watch -s &
pid=$!

echo "mimosa pid: $pid"

trap "echo '\nSTOP'; kill $pid; exit" SIGHUP SIGINT SIGTERM

echo 'send debug'
kill -s USR1 $pid

wait $pid

This script exits immediately, so does the mimosa process (I check it with grep again). The output in the console

$ ./debug.sh
mimosa pid: 79516
send debug
./debug.sh: line 11: 79516 User defined signal 1: 30 mimosa watch -s

What's wrong, how to fix?

Upvotes: 1

Views: 2524

Answers (1)

unhammer
unhammer

Reputation: 4740

Could mimosa be sending a signal to its own process group when you send the debug signal? That would explain it.

In interactive shells, doing ./program starts program with its own process group. If program does something like kill -s USR1 0, it'll never exit that group.

In non-interactive shells / scripts, doing ./program will start it as a child but in the same process group. If the child does kill -s USR1 0, it'll kill the calling script.

You could do trap 'echo ignoring' USR1 USR2 in your debug.sh in case those are the signals being sent by mimosa.

Alternatively, try turning on job control with set -m before starting mimosa.

See also I have "trap 'echo ignore' USR1" in my called script, why does the calling script get killed?

Upvotes: 2

Related Questions