我有一个问题,当作为cron作业执行时,将SCP消息重定向到文件:
(scp -q ${locationsFromTo[$i]} ${locationsFromTo[$i+1]}) >> $logFile 2>&1;
有没有办法将SCP错误重定向到文件?
这是脚本:
#!/bin/bash
#The script scp the files from a remote server
#The script will run only for ($sleepTime * $maxIterations) before exiting (default: 120 iteration, with sleep 5m = 10 hours)
declare -a locationsFromTo=("test@ssh.server.com:IN/*" "/home/in/." "test@ssh.server.com:OUT/*" "/home/out/.");
arraylength=${#locationsFromTo[@]};
logFile="/home/scp.log";
sleepTime=$((5 * 60));
iterationCount=0;
maxIterations=120;
echo "$(date) - Script is Started" > $logFile;
for (( i=0; i<${arraylength}; i+=2 ))
do
while true
do
echo "executing scp command" >> $logFile;
(scp -q ${locationsFromTo[$i]} ${locationsFromTo[$i+1]}) >> $logFile 2>&1;
if (($? == 0)); then
echo "$(date) - scp success" >> $logFile; break;
else
echo "scp command failed" >> $logFile;
if(($iterationCount >= $maxIterations-1)); then
echo "$(date) - script is running too long. exiting." >> $logFile;
break;
else
iterationCount=$iterationCount+1;
sleep $sleepTime
fi;
fi;
done;
done;
Cron作业来自$ logFile:
Wed Mar 25 09:10:01 EDT 2015 - Script is Started
executing scp command
scp command failed
来自$ logFile的手动执行结果:
Wed Mar 25 13:25:58 EDT 2015 - Script is Started
executing scp command
scp: fromOUAC/*: No such file or directory
scp command failed
答案 0 :(得分:0)
我刚删除'-q'标志以允许“scp”输出。 (我还必须包含密钥文件的路径)
显然scp检测到它从终端运行时是什么时候将它的输出更改为无声。 https://serverfault.com/questions/187620/scp-report-progress-to-cron-output
现在我的SCP命令&amp;脚本看起来像这样:
#!/bin/bash
declare -a locationsFromTo=("test@ssh.server.com:inDir/*" "/home/user/inDir/." "test@ssh.server.com:outDir/*" "/home/user/outDir/.");
arraylength=${#locationsFromTo[@]};
logFile="/home/user/logDir/scp.log";
keyFile="/home/user/.ssh/keyFile";
for (( i=0; i<${arraylength}; i+=2 ))
do
scp -i $keyFile ${locationsFromTo[$i]} ${locationsFromTo[$i+1]} >> $logFile 2>&1;
done;