2024-10-07 15:24:33 +08:00
|
|
|
import os
|
2024-10-06 22:12:47 +08:00
|
|
|
import subprocess
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
# 定义命令
|
|
|
|
command = "/home/star/anaconda3/envs/pfcfuse/bin/python /home/star/whaiDir/PFCFuse/train.py"
|
|
|
|
|
|
|
|
# 获取当前时间并格式化为文件名
|
|
|
|
current_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
|
output_file = f"/home/star/whaiDir/PFCFuse/logs/log_{current_time}.log"
|
|
|
|
|
2024-10-07 15:24:33 +08:00
|
|
|
try:
|
|
|
|
# 运行命令并将输出重定向到文件
|
|
|
|
with open(output_file, 'w') as file:
|
|
|
|
result = subprocess.run(command.split(), stdout=file, stderr=subprocess.STDOUT, check=True)
|
2024-10-06 22:12:47 +08:00
|
|
|
|
2024-10-07 15:24:33 +08:00
|
|
|
# 如果命令成功执行,则打印确认信息
|
|
|
|
print(f"Command executed successfully. Output has been written to {output_file}")
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
# 如果命令执行失败,则删除文件并打印错误信息
|
|
|
|
if os.path.exists(output_file):
|
|
|
|
os.remove(output_file)
|
|
|
|
print(f"Command failed with return code {e.returncode}. No log file was created.")
|