如何检查进程是否在python(linux)中运行?
我试图根据进程名test.py检查进程是否正在运行,然后退出如何检查进程是否在python(linux)中运行?,python,Python,我试图根据进程名test.py检查进程是否正在运行,然后退出 l = commands.getstatusoutput("ps aux |grep 'python2.7'| grep 'test.py' | grep -v 'grep' | wc -l") if int(l[1]) == 1: pid = commands.getstatusoutput("ps -ef |grep 'python2.7'| grep 'test.py ' |awk '{print $2}
l = commands.getstatusoutput("ps aux |grep 'python2.7'| grep
'test.py' | grep -v 'grep' | wc -l")
if int(l[1]) == 1:
pid = commands.getstatusoutput("ps -ef |grep 'python2.7'| grep 'test.py ' |awk '{print $2}'")
print "the process 'test.py ' have running ,the pid is :"+str(pid[1])
sys.exit(0)
else:
print "not running"
输出:
georgetovrea@dev:/test$ python2.7 test.py
the process 'test.py ' have running ,the pid is :26517
26542
dhruv@dhruvpathak:/tmp$ /usr/bin/python2.7 test.py
(0, '1')
the process 'test.py ' have running ,the pid is :15620
然后我试着检查一下
$ ps aux | grep agg_test
georgetovrea 25181 0.0 0.0 10944 932 pts/9 S+ 23:26 0:00 grep --color=auto test
进程未运行,我想检查process test.py是否已运行,然后退出,
但在进程运行时,总是打印
如何检查进程是否已运行?有什么建议吗?谢谢你的帮助。这个程序对我来说运行得很好。 我猜您是在用python而不是python2.7运行程序 在grep语句中尝试使用python而不是python2.7,那么罪魁祸首确实是您对python的使用 b将其更改回python2.7,并使用python2.7的完整路径运行程序 该方案:
import commands
import sys
l = commands.getstatusoutput("ps aux |grep 'python2.7'| grep 'test.py' | grep -v 'grep' | wc -l")
print(l)
if int(l[1]) == 1:
pid = commands.getstatusoutput("ps -ef |grep 'python2.7'| grep 'test.py ' |awk '{print $2}'")
print "the process 'test.py ' have running ,the pid is :"+str(pid[1])
sys.exit(0)
else:
print "not running"
输出:
georgetovrea@dev:/test$ python2.7 test.py
the process 'test.py ' have running ,the pid is :26517
26542
dhruv@dhruvpathak:/tmp$ /usr/bin/python2.7 test.py
(0, '1')
the process 'test.py ' have running ,the pid is :15620
DhruvPathak,我正在用python2.7运行程序,进程未运行,但运行程序以检查并获取消息get the process'test.py'have running,pid为:28373,python文件的名称是什么?在python中运行的命令与在shell中运行该命令相比是否有任何不同?Nick,运行test.py时,输出是进程“test.py”正在运行,pid是:26517 26542可能的重复。