0

This question already has an answer here:

Hello I have a bash script that is one liner for executing a piece of long running C++ code. Running ps -xaf the script is running and C++ code is its child

script.sh
\_ long_main.cpp

When i run the code from the command line and press ctrl+C the bash script and the c++ code are interrupted and stop the execution. I love that.

However when i run a script and execute kill -SIGINT PID on my script the script dies, and C++ code runs on. When i run ps -xaf i see that sh script is dead indeed, but C++ code is running on its own, and it is now a top level process like so:

long_main.cpp

Could you please elaborate on what is the difference between CRTL+C and kill -SIGINT in this case.

marked as duplicate by dessert, user535733, Eric Carvalho, Thomas Ward Mar 2 '18 at 14:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • with CTRL-C you kill your application, then the script exits. with kill, you send a signal to the script (and not to your application). So the continue running – pim Feb 28 '18 at 14:51
  • kill kills only single processes if you give it a PID, you have to specify a PGID (process group ID) to kill a process and its children, see the question I linked. pkill is a convenient way to simplify the task. Also related: What's the best way to send a signal to all members of a process group? – dessert Feb 28 '18 at 14:51
  • Read man 7 signal – waltinator Feb 28 '18 at 15:42
0

If the shell script in question only contains a single command that invokes an external program you can prepend the command with exec to replace the shell process instead of spawning a new process. E. g. change

/path/to/my-program "$@"

to

exec /path/to/my-program "$@"

Since my-program now replaces the shell process it retains its PID and receives all signals sent to it by PID incl. SIGINT. Obviously you can't send a signal to it based on the name or command-line of the shell script any longer.

Not the answer you're looking for? Browse other questions tagged or ask your own question.