Simulate Ctrl-C keyboard interrupt in Python while

2019-04-22 17:53发布

I am working on some scripts (in the company I work in) that are loaded/unloaded into hypervisors to fire a piece of code when an event occurs. The only way to actually unload a script is to hit Ctrl-C. I am writing a function in Python that automates the process

As soon as it sees the string "done" in the output of the program, it should kill the vprobe. I am using subprocess.Popen to execute the command:

lineList = buff.readlines()
cmd = "vprobe /vprobe/myhello.emt"
p = subprocess.Popen(args = cmd, shell=True,stdout = buff, universal_newlines = True,preexec_fn=os.setsid)
while not re.search("done",lineList[-1]):
        print "waiting"
os.kill(p.pid,signal.CTRL_C_EVENT)

As you can see, I am writing the output in buff file descriptor opened in read+write mode. I check the last line; if it has 'done', I kill it. Unfortunately, the CTRL_C_EVENT is only valid for Windows. What can I do for Linux?

3条回答
Root(大扎)
2楼-- · 2019-04-22 18:15

Maybe I misunderstand something, but the way you do it it is difficult to get the desired result.

Whatever buff is, you query it first, then use it in the context of Popen() and then you hope that by maciv lineList fills itself up.

What you probably want is something like

logfile = open("mylogfile", "a")
p = subprocess.Popen(['vprobe', '/vprobe/myhello.emt'], stdout=subprocess.PIPE,  buff, universal_newlines=True, preexec_fn=os.setsid)
for line in p.stdout:
    logfile.write(line)
    if re.search("done", line):
        break
    print "waiting"
os.kill(p.pid, signal.CTRL_C_EVENT)

This gives you a pipe end fed by your vprobe script which you can read out linewise and act appropriately upon the found output.

查看更多
小情绪 Triste *
3楼-- · 2019-04-22 18:16

I think you can just send the Linux equivalent, signal.SIGINT (the interrupt signal).

(Edit: I used to have something here discouraging the use of this strategy for controlling subprocesses, but on more careful reading it sounds like you've already decided you need control-C in this specific case... So, SIGINT should do it.)

查看更多
虎瘦雄心在
4楼-- · 2019-04-22 18:32

In Linux, Ctrl-C keyboard interrupt can be sent programmatically to a process using Popen.send_signal(signal.SIGINT) function. For example

import subprocess
import signal

..
process = subprocess.Popen(..)
..
process.send_signal(signal.SIGINT)
..

Don't use Popen.communicate() for blocking commands..

查看更多
登录 后发表回答