2017-05-31 3 views
0

Ich versuche, wenn der Prozess zu überprüfen, ist auf Prozessnamen (test.py) auf der Basis läuft, dann beendenWie zu überprüfen, haben Prozess läuft in Python (in 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" 

Ausgang:

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

dann versuche ich

$ ps aux | grep agg_test 
georgetovrea 25181 0.0 0.0 10944 932 pts/9 S+ 23:26 0:00 grep --color=auto test 
zu überprüfen

der Prozess läuft nicht, ich möchte überprüfen, ob der Prozess (test.py) ausgeführt haben, dann beenden, aber es ist immer drucken der Prozess läuft,

Wie überprüfe ich, ob der Prozess läuft? Irgendwelche Vorschläge? Danke für jede Hilfe.

+0

Ist der Befehl, den Sie‘ Re-Running von innerhalb Pythons Berichterstattung etwas anders versus wenn Sie nur diesen Befehl in der Shell ausführen? –

+0

Nick, wenn Sie die test.py ausführen, Ausgabe ist "der Prozess 'test.py' laufen, ist die PID: 26517 26542" – georgetovrea

+0

Mögliche Duplikat von [dies] (https://stackoverflow.com/questions/568271/how-to-check-wenn-existiert-ein-Prozess-mit-einem-gegebenen-PID-in-Python). – IsaacS

Antwort

-1

Das Programm läuft gut für mich. Ich denke, dass Sie das Programm mit python anstelle von python2.7 speziell ausführen.

(a) Versuchen Sie mit python anstelle von python2.7 in Ihrem Grep-Anweisung, dann ist der Schuldige in der Tat Ihre Verwendung von Python.

(b) es zu python2.7 ändern zurück und Ihrem Programm mit vollständigem Pfad

Das Programm zur python2.7 laufen:

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" 

Ausgang:

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

(0, '1') 

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

DhruvPathak, Ich führe das Programm mit Python2.7, der Prozess läuft nicht, aber läuft das Programm zu überprüfen und erhalten Sie die Nachricht "Get den Prozess 'test.py' laufen, die PID ist: 28373," – georgetovrea

+0

Was ist Der Name Ihrer Python-Datei? – DhruvPathak

Verwandte Themen