demo01.py 818 B

123456789101112131415161718192021222324252627
  1. import subprocess
  2. from datetime import datetime
  3. # 获取当前日期,并格式化为字符串
  4. now = datetime.now()
  5. date_str = now.strftime("%Y-%m-%d")
  6. # 定义日志文件名
  7. log_file = f"script_output_{date_str}.log"
  8. # 定义Shell脚本的路径
  9. script1_path = "./script1.sh"
  10. script2_path = "./script1.sh"
  11. with open(log_file, 'w') as f:
  12. # 调用script1.sh
  13. result = subprocess.run([script1_path], shell=True, stdout=f, stderr=subprocess.STDOUT)
  14. # 检查返回码
  15. if result.returncode == 0:
  16. print("script1.sh 执行成功")
  17. # 调用script2.sh
  18. result2 = subprocess.run([script2_path], shell=True, stdout=f, stderr=subprocess.STDOUT)
  19. print("script2.sh 执行成功")
  20. else:
  21. print("script1.sh 执行失败")
  22. print(f"错误输出: {result.stderr}")