检查守护进程是否正在运行
Check if daemon process is runnning
我是 运行 几个守护进程,我需要维护一个日志以在每 15 分钟的时间间隔后检查哪些进程是 运行。我尝试实现的代码是
def monitor_deamons():
print "starting monitor"
demo = ps -ef | grep -v grep | grep daemon #fails
print demo
PROCESSCOUNT=(ps -ef |grep -v grep |grep -cw <daemon name> #fails
print PROCESSCOUNT
我通过以下命令启动所有这些守护线程
python daemon_name.py start
我将 post 一些成功执行您的第一个命令的代码,您存储在 demo
中的结果,您可以从中推断如何获得 PROCESS_COUNT
.
import subprocess
def monitor_deamons():
print "starting monitor"
ps = subprocess.Popen(('ps', '-ef'), stdout=subprocess.PIPE)
grep = subprocess.Popen(('grep', '-v', 'grep'), stdin=ps.stdout, stdout=subprocess.PIPE)
ps.stdout.close() # Allow ps to receive a SIGPIPE if grep exits.
grep_daemon = subprocess.Popen(('grep', 'daemon'), stdin=grep.stdout)
grep.stdout.close() # Allow grep to receive a SIGPIPE if grep_daemon exits
output = grep_daemon.communicate()[0]
print output
monitor_deamons()
下次您 运行 遇到问题时,请说明您尝试过和搜索过的内容。如果您研究过如何从 Python 执行流程,您会发现子流程。从那里你可以用谷歌搜索如何在 Python 中的进程之间进行管道传输,并得出与我的类似的解决方案。
您还可以使用 python 中提供的 threading
模块。如果您 运行 在同一进程中执行以下命令,这将为您提供有关 daemon_name.py 进程创建的守护线程的更多信息。
获取线程数:
In [12]: threading.activeCount()
Out[12]: 26
列出所有活动线程:
In [5]: threading.enumerate()
Out[5]:
[<HistorySavingThread(IPythonHistorySavingThread, started 140361105082112)>,
<Thread(Thread-17, started daemon 140359321511680)>,
<Thread(Thread-9, started daemon 140359866775296)>,
<Thread(Thread-22, started daemon 140359279548160)>,
<Thread(Thread-5, started daemon 140360376239872)>,
<Thread(Thread-18, started daemon 140359313118976)>,
<Thread(Thread-10, started daemon 140359858382592)>,
<Thread(Thread-20, started daemon 140359296333568)>,
<Thread(Thread-21, started daemon 140359287940864)>,
<Thread(Thread-13, started daemon 140359833204480)>,
<Thread(Thread-16, started daemon 140359329904384)>,
<_MainThread(MainThread, started 140361348196160)>,
<Thread(Thread-19, started daemon 140359304726272)>]
访问任何线程并执行任何操作。
In [8]: t17 = threading.enumerate()[1]
In [9]: t17.
t17.daemon t17.getName t17.ident t17.isAlive t17.isDaemon t17.is_alive t17.join t17.name t17.run t17.setDaemon t17.setName t17.start
In [9]: t17.getName()
Out[9]: 'Thread-17'
希望对您有所帮助。
我是 运行 几个守护进程,我需要维护一个日志以在每 15 分钟的时间间隔后检查哪些进程是 运行。我尝试实现的代码是
def monitor_deamons():
print "starting monitor"
demo = ps -ef | grep -v grep | grep daemon #fails
print demo
PROCESSCOUNT=(ps -ef |grep -v grep |grep -cw <daemon name> #fails
print PROCESSCOUNT
我通过以下命令启动所有这些守护线程
python daemon_name.py start
我将 post 一些成功执行您的第一个命令的代码,您存储在 demo
中的结果,您可以从中推断如何获得 PROCESS_COUNT
.
import subprocess
def monitor_deamons():
print "starting monitor"
ps = subprocess.Popen(('ps', '-ef'), stdout=subprocess.PIPE)
grep = subprocess.Popen(('grep', '-v', 'grep'), stdin=ps.stdout, stdout=subprocess.PIPE)
ps.stdout.close() # Allow ps to receive a SIGPIPE if grep exits.
grep_daemon = subprocess.Popen(('grep', 'daemon'), stdin=grep.stdout)
grep.stdout.close() # Allow grep to receive a SIGPIPE if grep_daemon exits
output = grep_daemon.communicate()[0]
print output
monitor_deamons()
下次您 运行 遇到问题时,请说明您尝试过和搜索过的内容。如果您研究过如何从 Python 执行流程,您会发现子流程。从那里你可以用谷歌搜索如何在 Python 中的进程之间进行管道传输,并得出与我的类似的解决方案。
您还可以使用 python 中提供的 threading
模块。如果您 运行 在同一进程中执行以下命令,这将为您提供有关 daemon_name.py 进程创建的守护线程的更多信息。
获取线程数:
In [12]: threading.activeCount()
Out[12]: 26
列出所有活动线程:
In [5]: threading.enumerate()
Out[5]:
[<HistorySavingThread(IPythonHistorySavingThread, started 140361105082112)>,
<Thread(Thread-17, started daemon 140359321511680)>,
<Thread(Thread-9, started daemon 140359866775296)>,
<Thread(Thread-22, started daemon 140359279548160)>,
<Thread(Thread-5, started daemon 140360376239872)>,
<Thread(Thread-18, started daemon 140359313118976)>,
<Thread(Thread-10, started daemon 140359858382592)>,
<Thread(Thread-20, started daemon 140359296333568)>,
<Thread(Thread-21, started daemon 140359287940864)>,
<Thread(Thread-13, started daemon 140359833204480)>,
<Thread(Thread-16, started daemon 140359329904384)>,
<_MainThread(MainThread, started 140361348196160)>,
<Thread(Thread-19, started daemon 140359304726272)>]
访问任何线程并执行任何操作。
In [8]: t17 = threading.enumerate()[1]
In [9]: t17.
t17.daemon t17.getName t17.ident t17.isAlive t17.isDaemon t17.is_alive t17.join t17.name t17.run t17.setDaemon t17.setName t17.start
In [9]: t17.getName()
Out[9]: 'Thread-17'
希望对您有所帮助。