subprocess應用:python原樣輸出linux信息以及poll()返回值判斷


 

 一. subprocess輸出shell命令的值

 方法1:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import subprocess


# 與在命令窗口執行顯示效果相同,如有彩色輸出可保留,但不能返回結果
def run(command):
    subprocess.call(command, shell=True)


# 實時輸出但不可顯示彩色,可以返回結果
def sh(command, print_msg=True):
    p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    lines = []
    for line in iter(p.stdout.readline, b''):
        line = line.rstrip().decode('utf8')
        if print_msg:
            print(">>>", line)
        lines.append(line)
    return lines


#print('run():')
#run("ping www.baidu.com")
#cmd = 'certbot certonly --no-self-upgrade -d "*.smartfleet.inhandiot.com" -d "smartfleet.inhandiot.com" --manual --preferred-challenges dns --dry-run --manual-auth-hook "/root/workshop/certbot/au.sh python aly add" --manual-cleanup-hook "/root/workshop/certbot/au.sh python aly clean"'
print('\n\nsh():')
sh("free -h")
#sh(cmd)

 

 

寫入文件

>>> fdout=open("/tmp/ifconfig.log", 'a')
>>> fderr=open("/tmp/ifconfig-error.log", 'a')
>>> p=subprocess.Popen("ifconfig", stdout=fdout, stderr=fderr, shell=True)
>>> p.wait()

 

 從文件中取某行,這里取最后一行為例

#1. 從文件中取到最后一行
>>> os.popen("cat /tmp/test11.log").readlines()[-1].strip()
或者
with open("/tmp/test11.log", mode="r", encoding="UTF-8") as fd:
    x=fd.readlines()[-1]
    print(x)

 

 

 

方法2:

import subprocess

def
run_shell(shell):
cmd
= subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True, shell=True, bufsize=1) # 實時輸出 while True: line = cmd.stdout.readline() print(line, end='') if not line: break return cmd.returncode if __name__ == '__main__': print(run_shell("ping www.baidu.com"))
universal_newlines: 各種換行符都統一處理為"\n"
bufsize=1: 表示行緩沖

 

 

方法3

.readline可能導致卡死,官方推薦使用communicate,但是如果還是使用subprocess.PIPE,執行完命令后才能拿到標准輸出,替換成sys.stdout就能達到實時輸出效果,代碼附上

import subprocess
import sys


def run_shell(shell):
    cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True,
                           stdout=sys.stdout, universal_newlines=True, shell=True, bufsize=1)

    cmd.communicate()
    return cmd.returncode


if __name__ == '__main__':
    print(run_shell("ping www.baidu.com"))

 

https://www.cnblogs.com/chenqionghe/p/11532245.html

https://www.jb51.net/article/201484.htm

 

 

 

二. subprocess是否執行成功的狀態返回值

 

本例以一個不存在的目錄為例,來測試狀態返回值

import subprocess


p = subprocess.Popen("ls /root", stderr=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True, shell=True, bufsize=1)


err_msg=p.stderr.read()
print("錯誤輸出為:",err_msg)

cmd_status=p.poll()
print(cmd_status) # 目錄為/root時返回0, 目錄為不存在的/abc時返回2
while True: line = p.stdout.readline() print(line) if not line: break #return cmd_status

測試代碼如上,poll函數返回碼:

0 正常結束
1 sleep
2 子進程不存在
-15 kill
None 在運行

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM