1

I have a script that calls

#!/usr/bin/env bash

my_trap(){
  echo "signal was trapped";
}

trap my_trap INT
trap my_trap SIGINT
echo "here is the pid: $$"
echo "here is the parent id just in case: $(ps -o ppid= -p $$)"
tsc --watch # open a server / watch process

I call that script in terminal window 1. In another terminal window (terminal 2) I call

kill -INT <pid>

I call that for both the pid and parent pid echoed in the other terminal

nothing gets trapped - nothing gets logged ("signal was trapped" does not get logged)..

anyone know why SIGINT cannot be caught? Note that if I use ctrl-c in terminal 1, it does get caught, so something about sending SIGINT from another terminal window is not working.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 1
    Read [the tty demystified](http://www.linusakesson.net/programming/tty/) – Basile Starynkevitch Mar 04 '18 at 02:27
  • oh no that sounds no bueno lol – Alexander Mills Mar 04 '18 at 02:31
  • looks very good, but for other users short on time - is there any way to send a SIGINT signal to another process and trap it? – Alexander Mills Mar 04 '18 at 02:32
  • 1
    Consider using some other signal, perhaps `SIGUSR1`; read about [process group](https://en.wikipedia.org/wiki/Process_group)s – Basile Starynkevitch Mar 04 '18 at 02:36
  • thanks I see, this seems to relate: https://stackoverflow.com/questions/25668841/send-signal-between-scripts-bash – Alexander Mills Mar 04 '18 at 02:39
  • yeah I am trying to trap SIGUSR1 in the first script, and send that signal from the second sibling script, not sure if it will work, not working so far – Alexander Mills Mar 04 '18 at 02:45
  • BTW, signals are the worst [inter process communication](https://en.wikipedia.org/wiki/Inter-process_communication) facility. Why don't you design your code to use something better? – Basile Starynkevitch Mar 04 '18 at 02:46
  • this is pure bash code, for a library that's just written in bash, so, if you have a better idea, I am all for it – Alexander Mills Mar 04 '18 at 02:52
  • SIGUSR1 is sorta working, but I am getting this error: `Bus error: 10` looks like a lower level C related error – Alexander Mills Mar 04 '18 at 02:53
  • I guess I could set up a single named pipe and then each instance read from it, and it will received PIDs, and if the current script PID equals the next PID coming down the pipe I could do something – Alexander Mills Mar 04 '18 at 02:57
  • yeah named pipe won't work, because multiple processes will be writing to it and yeah that can't be handled easily / can't be multi-plexed...honestly IPC on most computers f*cking sucks...no wonder people tend to use network protocols instead... – Alexander Mills Mar 04 '18 at 05:30

0 Answers0