Let's say I have this script.sh
#!/bin/bash
exit_script() {
echo "Printing something special!"
echo "Maybe executing other commands!"
kill -- -$$ # Sends SIGTERM to child/sub processes
}
echo "Some other text"
#other commands here
sleep infinity
I want script.sh
to execute the function exit_script
whenever it receives SIGINT
or SIGTERM
For example:
killall script.sh # it will send SIGTERM to my script
and I want my script to execute this
exit_script() {
echo "Printing something special!"
echo "Maybe executing other commands!"
kill -- -$$ # Sends SIGTERM to child/sub processes
}
I tried to implement this feature using trap
trap exit_script SIGINT SIGTERM
The person who answered my question proved me wrong.
but it didn't work because trap
seems to react only to signals sent to child/sub processes. As a beginner I couldn't decipher trap
's man page so I probably missed the solution.
I guess that's what "real" programs like Chromium do when you send them SIGTERM
From https://major.io/2010/03/18/sigterm-vs-sigkill/
The application can determine what it wants to do once a SIGTERM is received. While most applications will clean up their resources and stop, some may not.