简体   繁体   English

发送SIGINT后从子进程捕获stdout

[英]Capturing stdout from subprocess after sending SIGINT

I have a dtrace snippet run via python script and the dtrace snippet is such that it generates data when CTRL-C is issued to it. 我有一个通过python脚本运行的dtrace代码段,并且dtrace代码段是这样的,它在向其发出CTRL-C时会生成数据。 So I had a signal_handler defined in the python script to catch CTRL-C from user and relay this to the dtrace invocation done via subprocess.Popen but I am unable to get any output in my log file. 因此,我在python脚本中定义了一个signal_handler来捕获用户的CTRL-C并将其中继到通过subprocess.Popen完成的dtrace调用,但是我无法在日志文件中获得任何输出。 Here is the script: 这是脚本:

Proc = []
signal_posted = False

def signal_handler(sig, frame):
    print("Got CTRL-C!")
    global signal_posted
    signal_posted = True
    global Proc
    Proc.send_signal(signal.SIGINT) #Signal posting from handler

def execute_hotkernel():
    #
    # Generate the .out output file
    #
    fileout = "hotkernel.out"
    fileo = open(fileout, "w+")

    global Proc
    Proc = subprocess.Popen(['/usr/sbin/dtrace', '-n', dtrace_script], stdout = fileo)
    while Proc.poll() is None:
        time.sleep(0.5)

def main():
    signal.signal(signal.SIGINT, signal_handler)        # Change our signal handler
    execute_hotkernel()

if __name__ == '__main__':
    main()

Since I have a file hotkernel.out set in subprocess.Popen command for stdout I was expecting the output from dtrace to be redirected to hotkernel.out on doing a CTRL-C but it is empty. 由于我在subprocess.Popen命令中为stdout设置了文件hotkernel.out,因此我期望dtrace的输出在执行CTRL-C时会重定向到hotkernel.out,但它为空。 What is missing here? 这里缺少什么?

I have a similar issue. 我有一个类似的问题。

In my case, it's a shell script that runs until you hit Control-C, and then prints out summary information. 就我而言,它是一个运行到您按下Control-C并输出摘要信息的shell脚本。 When I run this using subprocess.Popen, whether using a PIPE or a file object for stdout, I either don't get the information (with a file object) or it hangs when I try to run stdout.readline(). 当我使用subprocess.Popen运行此程序时,无论是使用PIPE还是将文件对象用于stdout,我要么得不到信息(带有文件对象),要么在尝试运行stdout.readline()时挂起。

I finally tried running the subprocess from the interpreter and discovered I could get the last line of output after the SIGINT with a PIPE if I call stdout.readline() (where it hangs) and hit Control-C (in the interpreter), and then call stdout.readline() again. 我最终尝试从解释器运行子进程,发现如果我调用stdout.readline()(挂起的地方)并按Control-C(在解释器中),则可以使用PIPE获得SIGINT之后的最后一行输出,并且然后再次调用stdout.readline()。

I do not know how to emulate this in script, for a file output or for a PIPE. 我不知道如何在脚本,文件输出或PIPE中模拟此内容。 I did not try the file output in the interpreter. 我没有尝试在解释器中输出文件。

EDIT: I finally got back to this and determined, it's actually pretty easy to emulate outside of python and really has nothing to do with python. 编辑:我终于回到这一点,并确定,在python之外进行仿真实际上很容易,并且与python无关。

/some_cmd_that_ends_on_sigint
(enter control-c)
*data from stdout in event handler*

Works 作品

/some_cmd_that_ends_on_sigint | tee some.log
(enter control-c)
*Nothing sent to stdout in event handler prints to the screen or the log*

Where's my log? 我的日志在哪里?

I ended up just adding a file stream in the event handler (in the some_cmd_that_ends_on_sigint source) that writes the data to a (possibly secondary) log. 我最终只是在事件处理程序中(在some_cmd_that_ends_on_sigint源中)添加了一个文件流,该数据流将数据写入(可能是辅助的)日志中。 Works, if a bit awkward. 可行,如果有点尴尬。 You get the data on the screen if running without any piping, but I can also read it when piped or from python from the secondary log. 如果在没有任何管道的情况下运行,则可以在屏幕上获取数据,但是当通过管道或从辅助日志中从python中读取时,我也可以读取数据。

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

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