I have an collection of scripts which are controlled by a main one. I want to trap the signal ctrl+c in the main script and propagate it to the others. The other scripts should trap this signal as well ( from the main script ) and do some clean-up ...
I have tried to send kill -s SIGINT
to the children, but they seem they are unable to catch the signal( even if trap 'Cleanup' SIGINT
being defined on the children scripts )
Any clues how to realize this?
The following example demonstrates a parent script that does something (
sleep 5
) after it starts two children that do their own thing (alsosleep 5
). When the parent exits (for whatever reason) it signals the children to terminate (don'tSIGINT
, termination is signaled bySIGTERM
, also the defaultkill
signal). The children then do their thing on reception ofSIGTERM
. If the children are scripts of their own, I recommend you change the trap onTERM
into a trap onEXIT
so that the children clean up no matter what the cause of their termination be (so long as it's trappable).Notice my use of
wait
. Bash does not interrupt running non-builtin commands when it receives a signal. Instead, it waits for them to complete and handles the signal after the command is done. If you usewait
, bash stops waiting immediately and handles the signal right away.I'm not sure what do you mean by "other scripts should trap this signal from the main script"? How can a subprocess script use code in the main script to trap a signal?
I don't want to try and write much code for you because I don't know exactly what you mean by "scripts controlled by a main one" either, but presumably you launch some subprocesses then have a control loop which checks if the other scripts have exited, and can grab their exit status? If that's the case, the thing that makes the most sense to me is for each script to do its own trapping and cleanup. When the main script traps a signal, it can if desired pass the signal along to all the children (via
kill -s <signal> pid
). When a child process traps a signal, it can return an exit status indicating that it was terminated by that signal. The main can then handle that exit status appropriately - perhaps in the same way as if it'd received that particular signal itself. (Shell functions are your friend.)Have you tried to : 1) Setup your traps in every script (master / childs) where you need them 2) Send to the master a kill with it's PID negated, to kill the whole process group, I mean :
man kill | grep -C1 Negative
MAIN PARENT SCRIPT HEADER BEFORE THE MAIN LOOP:::
In the main body of the script, as you spawn subprocesses you maintain an array with the proc ids of each. To load the PID into the array set the value to last spawned process e.g. put the following after each sub-shell spawn.
Contents of the M_shutdow would be something like...