简体   繁体   English

如何检查我的子进程是否仍在正常运行?

[英]How do I check my subprocess is still running correctly?

I am unable to post code for this sorry, but I am trying to run a python script at all times from another python script which creates a system tray.抱歉,我无法发布代码,但我正在尝试从另一个创建系统托盘的 python 脚本始终运行 python 脚本。 This system tray will show if the program is correctly running or not.此系统托盘将显示程序是否正确运行。

I have tried a few methods so far, the most promising method has been using something like:到目前为止,我已经尝试了一些方法,最有希望的方法是使用类似的方法:

p = subprocess.Popen([xxx, xxx], stdout=PIPE, stderr=PIPE) 

Then I check if stderr has any output meaning there's been an error.然后我检查stderr是否有任何 output 表示出现错误。

However, this only works when I deliberately make an error occur (using the wrong file path) and nothing happens when I use the correct file path as the program never terminates.但是,这仅在我故意发生错误(使用错误的文件路径)时才有效,并且当我使用正确的文件路径时没有任何反应,因为程序永远不会终止。

So the main issue I'm having is that because I want the program to be running at all times it never terminates unless there's an error.所以我遇到的主要问题是,因为我希望程序始终运行,除非出现错误,否则它永远不会终止。 I want to be able to check that is it running so the user can check the status on the system tray.我希望能够检查它是否正在运行,以便用户可以检查系统托盘上的状态。

In Unix OS we use certain system calls for process management.在 Unix 操作系统中,我们使用某些系统调用进行进程管理。 The process is started when its parent process executes a fork system call.该进程在其父进程执行 fork 系统调用时启动。 The parent process may then wait() for the child process to terminate either normally by using the exit() system call, or abnormally due to a fatal exception or signal (eg, SIGTERM, SIGINT, SIGKILL), the exit status is returned to the OS and a SIGCHLD signal is sent to the parent process.然后父进程可以等待子进程通过使用exit()系统调用正常终止,或者由于致命异常或信号(例如,SIGTERM、SIGINT、SIGKILL)而异常终止,退出状态返回到操作系统和 SIGCHLD 信号被发送到父进程。 The exit status can then be retrieved by the parent process via the wait system call to learn about what actually happened.然后,父进程可以通过等待系统调用检索退出状态,以了解实际发生的情况。 Cheers干杯

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

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