2017-05-31 19 views
0

私はプロセスをチェックする方法は、Pythonで実行しています(Linuxで)?

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" 

出力終了し、その後、プロセスは、プロセス名(test.py)に基づいて実行されているかどうかを確認しようとしています:その後、私は

$ ps aux | grep agg_test 
georgetovrea 25181 0.0 0.0 10944 932 pts/9 S+ 23:26 0:00 grep --color=auto test 
をチェックしよう

[email protected]:/test$ python2.7 test.py 
the process 'test.py ' have running ,the pid is :26517 
26542 

プロセスが実行されていない、プロセス(test.py)が実行されていることを確認して終了して終了します。 が表示されますが、常にプロセスが実行中です。

プロセスの実行状態を確認するにはどうすればよいですか?助言がありますか?助けてくれてありがとう。

+0

がコマンドで使用します」あなたがシェルでそのコマンドを実行した場合とは違う何かを報告しているPython内から実行していますか? –

+0

ニック、test.pyを実行すると、 "プロセス 'test.py'が実行中です.pidは次のとおりです:26517 26542" – georgetovrea

+0

[this](https://stackoverflow.com/questions/568271)の可能な複製/ pid-in-pythonで-a-process-with-a--a-process-with-there-exist-a-processが存在するかどうかを調べる)。 – IsaacS

答えて

-1

プログラムは正常に動作します。 具体的にはpython2.7ではなく、pythonでプログラムを実行していると思います。

(a)grepステートメントでpython2.7の代わりにpythonを使用してみると、実際には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" 

出力:

[email protected]:/tmp$ /usr/bin/python2.7 test.py 

(0, '1') 

the process 'test.py ' have running ,the pid is :15620 
+0

DhruvPathak、私はpython2.7でプログラムを実行していますが、プロセスは実行されていませんが、プログラムを実行して "get 'プロセス' test.py 'を実行していれば、pidは:28373、" – georgetovrea

+0

あなたのpythonファイルの名前? – DhruvPathak

関連する問題