1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import subprocess
- from datetime import datetime
- # 获取当前日期,并格式化为字符串
- now = datetime.now()
- date_str = now.strftime("%Y-%m-%d")
- # 定义日志文件名
- log_file = f"script_output_{date_str}.log"
- n1 = 1
- n2 = 5
- # 定义Shell脚本的路径
- script1_path = "./script1.sh"
- script2_path = "./script2.sh"
- # 打开日志文件准备写入
- with open(log_file, 'w') as f:
- # 调用script1.sh,不使用shell=True
- result1 = subprocess.run([script1_path, str(n1), str(n2)], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
- text=True)
- # 检查script1.sh的返回码,并写入输出到日志文件
- if result1.returncode == 0 and not result1.stderr:
- print("script1.sh 执行成功")
- f.write(f"--- script1.sh 输出 ---\n{result1.stdout}\n")
- # 调用script2.sh,同样不使用shell=True
- result2 = subprocess.run([script2_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
- # 检查script2.sh的返回码,并写入输出到日志文件
- if result2.returncode == 0 and not result2.stderr:
- print("script2.sh 执行成功")
- f.write(f"--- script2.sh 输出 ---\n{result2.stdout}\n")
- else:
- print("script2.sh 执行失败")
- f.write(f"--- script2.sh 错误输出 ---\n{result2.stderr}\n")
- else:
- print("script1.sh 执行失败")
- f.write(f"--- script1.sh 错误输出 ---\n{result1.stderr}\n")
|