在SIGINT上杀死后台进程 [英] Kill background process on SIGINT

查看:80
本文介绍了在SIGINT上杀死后台进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本,它将在Windows的Bash下启动一个保持活动状态的脚本和Sublime Text 3:

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

此代码生成:

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

此脚本的全部目的是在Windows的Bash下使用Xorg服务器和GUI运行Sublime Text 3.我尝试创建一个快捷方式以使用"C:\ Windows \ System32 \ bash.exe" -c"./my-script.sh" 命令启动此脚本,并且必须在Windows中运行Bash使用ST3时的背景,因为否则会出现 dbus 错误(即使您修改了/etc/dbus-1/session.conf )

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)

问题是:如果我按 CTRL + C ,则 tail 进程仍在后台显示.

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

我更改了脚本的顺序,将 tail wait 放在了所有内容下面.

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

已解决:

看起来像 dash 不支持 SIGINT INT .解决方案是使用 bash SIGINT 可以正常工作.

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

推荐答案

看起来像 dash 不支持 SIGINT INT (位于至少在Windows的Linux子系统中就是这种情况.解决方案是使用 bash SIGINT 可以正常工作.

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:

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

sublime.cmd

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

这篇关于在SIGINT上杀死后台进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆