如何发送从bash脚本控制+ C? [英] How to send control+c from a bash script?
问题描述
我开始在bash脚本的一些画面,然后在运行Django的的runserver
命令他们每个人。我希望能够以编程方式阻止他们都管不好,这就要求我送控制+ C
到的runserver
。
I'm starting a number of screens in a bash script, then running django's runserver
command in each of them. I'd like to be able to programmatically stop them all as well, which requires me to send Control+c
to runserver
.
我怎样才能从我的bash脚本发送这些按键?
How can I send these keystrokes from my bash script?
推荐答案
<大骨节病>按Ctrl + C 骨节病>发送 SIGINT
信号。
杀-INT&LT; PID&GT;
发送 SIGINT
信号过:
# Terminates the program (like Ctrl+C)
kill -INT 888
# Force kill
kill -9 888
假设 888
是进程标识。
注意杀888
发送 SIGTERM
信号,这是略有不同,但还会询问程序停止。所以,如果你知道你在做什么(无处理程序绑定到 SIGINT
在程序中),一个简单的杀
就足够了
Note that kill 888
sends a SIGTERM
signal, which is slightly different, but will also ask for the program to stop. So if you know what you are doing (no handler bound to SIGINT
in the program), a simple kill
is enough.
要获得脚本推出的最后一个命令的PID,使用 $
:
To get the PID of the last command launched in your script, use $!
:
# Launch script in background
./my_script.sh &
# Get its PID
PID=$!
# Wait for 2 seconds
sleep 2
# Kill it
kill $PID
这篇关于如何发送从bash脚本控制+ C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!