如何使用python将控件C发送到Mac Terminal? [英] How to send control C to Mac Terminal using python?
问题描述
我有一个python脚本,需要将控件C发送到mac终端.我尝试发送纯文本"^ C",但我发现终端无法识别该命令. (终端表示python创建的伪终端)
I have a python script that needs to send control C to the mac terminal. I've tried sending the plain text "^C" but I get back that the terminal does not recognize the command. (The terminal meaning the pseudo terminal that python creates)
基本上,我正在使用终端运行旧的Unix可执行文件,并且我认为可以优雅地终止此操作的唯一方法是发送中断信号.有什么办法可以使终端机误以为我按下了控件C?
Basically, I am using the terminal to run an old Unix Executable and the only way that I can think of to terminate this gracefully is to send the interrupt signal. Is there any way I can fool the terminal into thinking that I pressed control C?
提前谢谢!
推荐答案
You can explicitly send the SIGINT signal to the process if you can get its PID using os.kill
.
os.kill(pid, signal.SIGINT)
这将要求您检测脚本以获取进程PID,但这是模拟"ctrl-c"行为的最佳方法.
This will require you to instrument your script to grab the process PID, but it's the best way to emulate the "ctrl-c" behavior.
这篇关于如何使用python将控件C发送到Mac Terminal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!