python脚本
import subprocess
import datetime
import time
def run_scp_command(source_path, target_path):
command = ['scp -r ', source_path, target_path]
try:
subprocess.run(command, check=True)
print("File transferred successfully!")
except subprocess.CalledProcessError as e:
print("An error occurred:", e)
while True:
dt_start = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
source_path = 'xx.xx.xx.xx:/root/Algo/dt_resource/dtt_dg_prod/resource/scada/100_20221221_transient'
target_path = './' + dt_start
run_scp_command(source_path, target_path)
time.sleep(5*60)
shell脚本
#!/bin/bash
# 函数来执行scp命令
run_scp_command() {
local source_path=$1
local target_path=$2
scp -r "$source_path" "$target_path"
if [ $? -eq 0 ]; then
echo "File transferred successfully!"
else
echo "An error occurred."
fi
}
# 无限循环
while true; do
# 获取当前时间并格式化
dt_start=$(date +"%Y%m%d%H%M%S")
# 定义源文件路径和目标路径
source_path='xx.xx.xx.xx:/root/Algo/dt_resource/dtt_dg_prod/resource/scada/100_20221221_transient'
target_path="./${dt_start}"
# 运行scp命令
run_scp_command "$source_path" "$target_path"
# 等待5分钟
sleep 300
done
添加权限
chmod +x download.sh
执行
./download.sh
Python与Shell脚本实现定时文件传输与错误处理
文章讲述了如何使用Python和shell脚本编写一个定时任务,通过scp命令从远程服务器同步文件到本地,并处理可能发生的错误。脚本包括一个Python函数和一个无限循环的shell脚本,用于定期将特定目录下的文件复制到当前日期命名的本地目录。
1350

被折叠的 条评论
为什么被折叠?



