I have a signal handler in C++ program:
void term(int signum)
{
cout << "in term" << endl;
# Do Stuff
exit(0);
}
int main(int argc, char *argv[])
{
# Do Stuff
signal(SIGINT, term);
# Do Stuff
}
The program works fine. goes in the function term when I press Ctrl+C and exits.
However, when I try to replicate the behavior using a script, it does not work.
Simplified version of the script I am running:
! /bin/bash
sudo ./program &
rpid=$!
sleep 2
sudo kill -INT $rpid
# sudo kill -TERM $rpid
# sudo kill $rpid
echo "killed"
wait $rpid
The program is does not even go in the term function until I press Ctrl+C. It is stuck on wait indefinitely.
kill -INT
? The body is about the difference between C and a shell script. OR did I miss-read it (In which case it is confusing). – ctrl-alt-delor Jan 10 '20 at 17:08exit(3)
from a signal handler c) usingsignal()
(which nobody actually knows how it works) instead ofsigaction(2)
d) pointlessly usingsudo
, etc. Please submit a reproducible example. – Jan 10 '20 at 17:09write(1, "in term\n", 8);
Anyways, try submitting a complete reproducible example. – Jan 10 '20 at 17:15SIGINT
signal to the entire process group (job), whilekill -INT $!
will only send it to the process group leader. Ifsudo
is actually waiting for the command it runs (I have no idea whether that's the case), onlysudo
will be killed, not the child it waits for. – Jan 10 '20 at 17:21man signal-safety
– Jan 10 '20 at 17:22sudo
is ignoring SIGINT, and your./program
, a child of sudo, cannot undo that. – Mark Plotnick Jan 10 '20 at 17:34kill -INT -$!
(with the negated pid) may work, but nobody really knows as long as you don't submit a reproducible testcase. – Jan 10 '20 at 17:35kill(2)
manpage. – Jan 10 '20 at 17:51