demo01.py 851 B

123456789101112131415161718192021222324252627282930
  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. n1 = 1
  9. n2 = 5
  10. # 定义Shell脚本的路径
  11. script1_path = "./script1.sh"
  12. script2_path = "./script2.sh"
  13. with open(log_file, 'w') as f:
  14. # 调用script1.sh
  15. result = subprocess.run([script1_path, str(n1), str(n2)], shell=True, stdout=f, stderr=subprocess.STDOUT)
  16. # 检查返回码
  17. if result.returncode == 0:
  18. print("script1.sh 执行成功")
  19. # 调用script2.sh
  20. result2 = subprocess.run([script2_path], shell=True, stdout=f, stderr=subprocess.STDOUT)
  21. print("script2.sh 执行成功")
  22. else:
  23. print("script1.sh 执行失败")
  24. print(f"错误输出: {result.stderr}")