Issue
I have a basic bash script that prints out its pid, then counts to 100:
echo $$
for i in {1..100}
do
echo $i
sleep 1
done
When I press ctrl+c, while this is running, the program will halt with an exit code of 130. I am trying to send a signal that will simulate a ctrl+c and cause the program to halt and exit with code 130.
When I type "kill -INT " from a different terminal window, nothing happens, though from what I understand, it should be the same as hitting ctrl+c. However, when I type "kill -9 ", I can successfully terminate the program, albeit with a different exit code. How do I stop my program with an interrupt signal, so it exits with the proper code?
Solution
Prepend process id with dash sign "-"
kill -SIGINT -<pid>
This will kill the process with exit code 130.
UPD: Why not to use SIGTERM(15) which is send by kill command by default (with no signal number or name)?
Answered By - Yuri Ginsburg