使用npm install时,由于npm install控制台输出的构建信息是加载条,之前用的python脚本不能输出,且加载条完之后的输出也不能获取。因为需要使用新的脚本,使用下面的脚本python执行npm install 可以输出加载条之后的日志。process.poll()为返回码,正确运行返回码为0,若不为0则退出系统。while True进入一个无限循环,用于持续地读取子进程的标准输出
runDeploynew.py #!/usr/bin/env python3 #coding: utf-8 import subprocess import sys def run_deploy_new(command): process = subprocess.Popen(command,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) while True: output = process.stdout.readline() if output == b'' and process.poll() is not None: break if output: # 输出加载条信息 print(output.decode().strip()) if process.poll() != 0: sys.exit(1) #if __name__ == "__main__": # run_deploy_new("npm install")
调用该脚本,其中command可以是任何shell命令。
#!/usr/bin/env python3 #coding: utf-8 from runDeploynew import run_deploy_new def fun1(): command = 'npm install' run_deploy_new(command)
标签:npm,输出,shell,python,__,process,command,install,日志 From: https://www.cnblogs.com/xiaoxiaomuyuyu/p/18102212