84669 Lernen von Personen
152542 Lernen von Personen
20005 Lernen von Personen
5487 Lernen von Personen
7821 Lernen von Personen
359900 Lernen von Personen
3350 Lernen von Personen
180660 Lernen von Personen
48569 Lernen von Personen
18603 Lernen von Personen
40936 Lernen von Personen
1549 Lernen von Personen
1183 Lernen von Personen
32909 Lernen von Personen
python3 脚本中有如下代码, 但 os.system()方法无法获取 shell 指令的返回值, 无法判断是否存在nginx的进程. 请问大神有什么方法可以解决该问题?
import os os.system('netstat -tnlp | grep nginx')
欢迎选择我的课程,让我们一起见证您的进步~~
怎么没有返回值了
import os if(os.system('netstat -tnlp | grep nginx') == 0) { print 'process nginx exists.' }
或者你想说的是system不能获取shell指令输出的内容?那就用popen呗
import os if(os.popen('netstat -tnlp | grep nginx').read() != '') { print 'process nginx exists.' }
调用子程序更强大的是subprocess.Popen,是这里不表,你的需求用这个实现有点复杂,想了解可以去查文档
subprocess.Popen
subprocess.getstatusoutput(cmd)
>>> subprocess.getstatusoutput('ls /bin/ls') (0, '/bin/ls') >>> subprocess.getstatusoutput('cat /bin/junk') (256, 'cat: /bin/junk: No such file or directory') >>> subprocess.getstatusoutput('/bin/junk') (256, 'sh: /bin/junk: not found')
怎么没有返回值了
或者你想说的是system不能获取shell指令输出的内容?那就用popen呗
调用子程序更强大的是
subprocess.Popen
,是这里不表,你的需求用这个实现有点复杂,想了解可以去查文档subprocess.getstatusoutput(cmd)