简体   繁体   English

如何使用 python 向 iTerm2 session 中的正在运行的进程发送 SIGINT 信号?

[英]How do I send a SIGINT signal to a running process in an iTerm2 session using python?

I'm trying to automate the restarting of several servers running in iTerm2 tabs using iTerm2's Python API .我正在尝试使用 iTerm2 的Python API自动重启在 iTerm2 选项卡中运行的几台服务器。 I can get a reference to the sessions, and issue commands to them (with async_send_text ), but I can't figure out how to send SIGINT signals to them so I can issue the restart commands.我可以获得对会话的引用,并向它们发出命令(使用async_send_text ),但我不知道如何向它们发送 SIGINT 信号,以便发出重启命令。

I've seen a few answers using AppleScript, but it seems like this should be possible in python.我已经看到一些使用 AppleScript 的答案,但似乎这在 python 中应该是可能的。

You can use os.kill() to send signals to a process as long as you have the process ID:只要您有进程 ID,就可以使用os.kill()向进程发送信号:

import os
import sys

os.kill(<PID>, signal.SIGINT)

This should work even when the process is in running in a terminal emulator.即使进程在终端模拟器中运行,这也应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM