gpt4 book ai didi

linux - 在 SIGINT 上杀死后台进程

转载 作者:太空宇宙 更新时间:2023-11-04 10:25:10 25 4
gpt4 key购买 nike

我有这个脚本,它应该在 Bash for Windows 下启动一个 keep-alive 脚本和 Sublime Text 3:

#!/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

此代码生成:

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

这个脚本的全部目的是我在 Bash for Windows 下运行带有 GUI 的 Sublime Text 3 和 Xorg 服务器。我尝试使用 "C:\Windows\System32\bash.exe"-c "./my-script.sh" 命令创建一个快捷方式来启动此脚本,Windows 的 Bash 必须在我使用 ST3 时的背景,否则会出现 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

# 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"

关于linux - 在 SIGINT 上杀死后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41926123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com