在 SIGINT 上杀死后台进程

Kill background process on SIGINT

我有这个脚本,它应该在 Bash 下启动一个 keep-alive 脚本和 Sublime Text 3 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

# 
trap "kill ${pid}; exit 1" INT
wait

此代码生成:

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

我 运行 带有 GUI 的 Sublime Text 3 和 Xorg 服务器在 Bash 下 Windows 的整个目的。当我使用 ST3 时,我尝试使用 "C:\Windows\System32\bash.exe" -c "./my-script.sh" 命令创建一个快捷方式来启动此脚本,而 Windows 的 Bash 必须在后台 运行,否则会有 dbus 错误(即使你修改了 /etc/dbus-1/session.conf

问题是:如果我按下 CTRL+Ctail 进程仍然出现在后台。

编辑:

我更改了脚本中的顺序,将 tailwait 放在所有内容下。

已解决:

看起来 dash 不支持 SIGINTINT。解决方案是使用 bash 并且 SIGINT 将正常工作。

看起来 dash 不支持 SIGINTINT(至少在 Windows 的 Linux 子系统中是这样)。解决方案是使用 bash 并且 SIGINT 将正常工作。

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

# 
# 
trap "kill ${pid[0]}; exit 1" SIGINT

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

wait

sublime.cmd

@ECHO OFF
"C:\Windows\System32\bash.exe" -c "./sublime.sh"