Home:ALL Converter>Kill background process on SIGINT

Kill background process on SIGINT

Ask Time:2017-01-30T05:23:37         Author:Lanti

Json Formatter

I have this script, which should start a keep-alive script and Sublime Text 3 under Bash for Windows:


set -e

# Keep alive (in background)
tail -f /dev/null &

echo "tail process id: ${pid}"
echo "Keep-alive process started with Sublime Text 3\nPress SIGINT (CTRL+C) to kill it..."

# Start Sublime Text 3
DISPLAY=localhost:0 /usr/bin/sublime

# http://stackoverflow.com/a/19274804/1442219
trap "kill ${pid}; exit 1" INT

This code generates:

tail process id: 49
Keep-alive process started with Sublime Text 3
Press SIGINT (CTRL+C) to kill it...
tail: cannot determine location of '/dev/null'. reverting to polling: Invalid argument

The whole purpose of this script that I run Sublime Text 3 with GUI with Xorg server under Bash for Windows. I try to make a shortcut to start this script with "C:\Windows\System32\bash.exe" -c "./my-script.sh" command and Bash for Windows have to run in the background when I use ST3, because otherwise there are dbus errors (even if you modified /etc/dbus-1/session.conf)

The problem is: If I press CTRL+C, tail process is still presented in the background.


I changed the ordering in the script, putted tail and wait under everything.


Looks like dash isn't supporting SIGINT or INT. Solution is to use bash and SIGINT will work as it should.

Author:Lanti,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/41926123/kill-background-process-on-sigint