We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
要启动/停止 python 子进程,您可以使用 subprocess 模块。
要检查它们是否正在运行,您可以使用 psutil:
...或者这个(它还会检查是否PID已被重用):
To start/stop python sub processes you can use the subprocess module.
To check whether they are running you might use psutil:
...or this (it will also check if the PID has been reused):
通过查看 /proc 目录的内容来检查正在运行的进程列表(即使是通过“ps”等核心实用程序)。
因此,您对查询正在运行的进程感兴趣的库与用于处理任何其他文件和目录的库相同(即
sys
或os
,具体取决于风格不过,请特别注意 os.path,它可以完成您所追求的大部分功能)。要终止进程或以其他方式与进程交互,您可以向它们发送信号,这是通过 os.kill 完成的。最后,您使用 os.popen 和朋友启动新进程。Checking the list of running processes is accomplished (even by core utilities like "ps") by looking at the contents of the /proc directory.
As such, the library you're interested for querying running processes is the same as used for working with any other files and directories (i.e.
sys
oros
, depending on the flavor you're after. Pay special attention toos.path
though, it does most of what you're after). To terminate or otherwise interact with processes, you send them signals, which is accomplished withos.kill
. Finally, you start new processes usingos.popen
and friends.既然您说这是一个 Linux 服务器,那么调用外部 ps 二进制文件通常比直接使用 /proc 中的信息更慢、使用更多资源并且更容易出错。
由于没有其他人提到,一种简单的方法是:
祝你好运。
Since you said this is a Linux server, calling the external ps binary is usually slower, uses more resources and is more error prone than using the information from /proc directly.
Since nobody else mentioned, one simple way is:
Good luck.
这就是我用的。它使用 procfs(所以你仅限于类 Unix 系统,我认为不适用于 Mac)和前面提到的 glob。它还获取命令行,使您可以识别进程。要终止进程,您可以使用 os.kill(signal.SIGTERM, pid) 。要使用子进程,请查看这篇文章 Python、Popen 和 select - 等待进程终止或超时
This is what i use. It uses procfs (so you are limited to Unix like systems, will not work on macs i think) and the previously mentioned glob. It also gets the cmdline, which allows you to identify the process. For killing the process you can use
os.kill(signal.SIGTERM, pid)
. For using subprocess, please check this post Python, Popen and select - waiting for a process to terminate or a timeoutos 模块可能是你的朋友。例如,有 os.kill 来终止进程。
在获取进程列表方面,您可能需要使用
ps
命令。 这个问题有更多相关信息。The os module is probably your friend. There's
os.kill
, for instance to kill a process.In terms of getting a list of processes, you'll probably want to shell out to the
ps
command. This question has more information on that.Python 子进程 http://docs.python.org/library/subprocess.html 可能帮助你。如果您创建一个带有子进程的进程,您可以使用 Popen.terminate() 函数来停止它。
Python subprocess http://docs.python.org/library/subprocess.html might help you. If you create a process with subprocess, you can use Popen.terminate() function to stop it.
我会使用 PSutil。提供一个实际示例:
或者,Red Hat 使用并维护一个名为 python-linux-procfs 的 Python 模块,该模块本机解析 /proc 来管理进程。它没有得到很好的宣传,但提供了一些额外的 Linux 特定功能(例如,调度类),这些功能有时很有用。
http://pkgs.fedoraproject.org/gitweb/?p=python -linux-procfs.git
I'd use PSutil. To provide a practical example:
Alternatively, Red Hat use and maintain a Python module called python-linux-procfs , which natively parses /proc, to manage processes. it's not very well publicized, but provides some additional Linux-specific features (eg, scheduling class) which are sometimes useful.
http://pkgs.fedoraproject.org/gitweb/?p=python-linux-procfs.git
探索 Supervisor,它是一个基于 Python 的 Linux 过程控制系统。它提供了一个基于 Web 的 UI,可以在崩溃时自动检查进程状态/启动/停止/重新启动。
另外还有 superlance 插件,您可以在进程崩溃时发送邮件。
Explore Supervisor which is a Python based process control system for Linux. It gives a web based UI to check the process status/start/stop/restart automatically on crash.
Also there are superlance plugins available where you can send mail in case of process crash.