如何从 bash 脚本发送 control+c?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5789642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:28:07  来源:igfitidea点击:

How to send control+c from a bash script?

bashgnu-screen

提问by ghickman

I'm starting a number of screens in a bash script, then running django's runservercommand in each of them. I'd like to be able to programmatically stop them all as well, which requires me to send Control+cto runserver.

我在 bash 脚本中启动了多个屏幕,然后runserver在每个屏幕中运行 django 的命令。我也希望能够以编程方式停止它们,这需要我发送Control+crunserver.

How can I send these keystrokes from my bash script?

如何从我的 bash 脚本发送这些击键?

回答by Matthieu Napoli

Ctrl+Csends a SIGINTsignal.

Ctrl+C发出SIGINT信号。

kill -INT <pid>sends a SIGINTsignal too:

kill -INT <pid>也发送SIGINT信号:

# Terminates the program (like Ctrl+C)
kill -INT 888
# Force kill
kill -9 888

Assuming 888is your process ID.

假设888是您的进程 ID。



Note that kill 888sends a SIGTERMsignal, 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 SIGINTin the program), a simple killis enough.

请注意,kill 888发送SIGTERM信号略有不同,但也会要求程序停止。因此,如果您知道自己在做什么(程序中没有绑定处理SIGINT程序),那么简单kill就足够了。

To get the PID of the last command launched in your script, use $!:

要获取脚本中启动的最后一个命令的 PID,请使用$!

# Launch script in background
./my_script.sh &
# Get its PID
PID=$!
# Wait for 2 seconds
sleep 2
# Kill it
kill $PID

回答by paxdiablo

CTRL-Cgenerally sends a SIGINT signal to the process so you can simply do:

CTRL-C通常向进程发送 SIGINT 信号,因此您可以简单地执行以下操作:

kill -INT <processID>

from the command line (or a script), to affect the specific processID.

从命令行(或脚本),以影响特定的processID.

I say "generally" because, as with most of UNIX, this is near infinitely configurable. If you execute stty -a, you can see which key sequence is tied to the intrsignal. This will probably be CTRL-Cbut that key sequence may be mapped to something else entirely.

我说“一般”是因为,与大多数 UNIX 一样,这几乎是可无限配置的。如果您执行stty -a,您可以看到哪个键序列与intr信号相关联。这可能是CTRL-C但该键序列可能完全映射到其他内容。



The following script shows this in action (albeit with TERMrather than INTsince sleepdoesn't react to INTin my environment):

以下脚本显示了这一点(尽管在我的环境中没有反应,TERM而不是INT因为sleep没有反应INT):

#!/usr/bin/env bash

sleep 3600 &
pid=$!
sleep 5

echo ===
echo PID is $pid, before kill:
ps -ef | grep -E "PPID|$pid" | sed 's/^/   /'
echo ===

( kill -TERM $pid ) 2>&1
sleep 5

echo ===
echo PID is $pid, after kill:
ps -ef | grep -E "PPID|$pid" | sed 's/^/   /'
echo ===

It basically starts an hour-log sleepprocess and grabs its process ID. It then outputs the relevant process details before killing the process.

它基本上启动一个小时日志sleep进程并获取其进程 ID。然后在杀死进程之前输出相关的进程详细信息。

After a small wait, it then checks the process table to see if the process has gone. As you can see from the output of the script, it is indeed gone:

稍等片刻后,它会检查进程表以查看进程是否已消失。从脚本的输出中可以看出,它确实消失了:

===
PID is 28380, before kill:
   UID   PID     PPID    TTY     STIME      COMMAND
   pax   28380   24652   tty42   09:26:49   /bin/sleep
===
./qq.sh: line 12: 28380 Terminated              sleep 3600
===
PID is 28380, after kill:
   UID   PID     PPID    TTY     STIME      COMMAND
===

回答by Muhammad Karam Shehzad

You can get the PID of a particular process like MySQL by using following commands: ps -e | pgrep mysql

您可以使用以下命令获取特定进程(如 MySQL)的 PI​​D: ps -e | pgrep mysql

This command will give you the PID of MySQL rocess. e.g, 13954 Now, type following command on terminal. kill -9 13954 This will kill the process of MySQL.

此命令将为您提供 MySQL 进程的 PID。例如,13954 现在,在终端上输入以下命令。kill -9 13954 这将杀死 MySQL 的进程。

回答by user2176228

    pgrep -f process_name > any_file_name
    sed -i 's/^/kill /' any_file_name
    chmod 777 any_file_name
    ./any_file_name

for example 'pgrep -f firefox' will grep the PID of running 'firefox' and will save this PID to a file called 'any_file_name'. 'sed' command will add the 'kill' in the beginning of the PID number in 'any_file_name' file. Third line will make 'any_file_name' file executable. Now forth line will kill the PID available in the file 'any_file_name'. Writing the above four lines in a file and executing that file can do the control-C. Working absolutely fine for me.

例如'pgrep -f firefox' 将grep 运行'firefox' 的PID 并将这个PID 保存到一个名为'any_file_name' 的文件中。'sed' 命令将在 'any_file_name' 文件中的 PID 编号的开头添加 'kill'。第三行将使 'any_file_name' 文件可执行。现在第四行将终止文件“any_file_name”中可用的 PID。将以上四行写在一个文件中并执行该文件可以做control-C。对我来说绝对没问题。