0

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

#!/bin/dash

set -e

# Keep alive (in background)
tail -f /dev/null &
pid=$!

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
wait

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.

Edit:

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

Solved:

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

Lanti
  • 2,299
  • 2
  • 36
  • 69
  • Possible duplicate of [How do I kill background processes / jobs when my shell script exits?](http://stackoverflow.com/questions/360201/how-do-i-kill-background-processes-jobs-when-my-shell-script-exits) – jww Jan 29 '17 at 21:42
  • 1
    Your `wait` command causes the script to pause and wait for all background jobs to be finished. So your `tail` command has to finish before the `sublime` command is launched. Is that your intent? As for the problem with `tail`, maybe you could try using `sleep` with a very long duration? – Fred Jan 29 '17 at 21:50
  • No, `tail` command proves that `Bash for Windows`, aka Linux Subsystem for Win10 should run in the background parallel with ST3, because of `dbus` issues. If you close ST3, that's souldn't effect this script, at least this not what I intend to do on the first place. – Lanti Jan 29 '17 at 22:15
  • Looks like `dash` isn't supporting `SIGINT` or `INT`. Solution is to use `bash` and `SIGINT` will work. – Lanti Jan 29 '17 at 22:40

1 Answers1

2

Looks like dash isn't supporting SIGINT or INT (at least this is the case in the Linux subsystem for Windows). Solution is to use bash and SIGINT will work as it should.

sublime.sh:

#!/bin/bash

set -e

# Just to make sure, this line takes PID1 in Docker environments
# So SIGINT/SIGKILL won't be blocked
echo "pid1" > /dev/null

echo -e "\n///////////////////////////////////////////////\n"

# Keep alive (in background) hack from Docker environments
tail -f /dev/null &
pid[0]=$!

echo "tail process id: ${pid[0]}"
echo -e "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
# http://stackoverflow.com/a/360275/1442219
trap "kill ${pid[0]}; exit 1" SIGINT

echo -e "\n///////////////////////////////////////////////\n"

wait

sublime.cmd

@ECHO OFF
"C:\Windows\System32\bash.exe" -c "./sublime.sh"
Lanti
  • 2,299
  • 2
  • 36
  • 69