time + sftp 命令脚本:如何正确地将输出重定向到日志文件

time + sftp commands script: how to correctly redirect output to log file

我构建了一个脚本来自动执行每日 ftp 下载过程。为了使它更简单,类似于以下内容:

#!/usr/bin/env bash
time sshpass -p "mypass" sftp myuser@sftp.domain.com:/myremotepath/myfile.txt.gz /mylocalpath/myfile.txt.gz 
time sshpass -p "mypass" sftp myuser@sftp.domain.com:/myremotepath/myfile2.txt.gz /mylocalpath/myfile2.txt.gz 

我执行如下脚本以将输出行重定向到日志文件:

./sftp_test.sh  > /mylogpath/sftp_test.log 2>1&

但它没有按预期工作。仅保存“Fetching /myremotepath/myfile2.txt.gz to /mylocalpath/myfile2.txt.gz”等行。不是时间命令输出。不是其他 sftp 行。

如果我在没有“> /mylogpath/sftp_test.log 2>1&”部分的情况下手动执行脚本,我想完全按照我看到的那样保存输出。

我也尝试了 tee 命令但没有成功。

我该怎么做?

脚本可能是一个选项:

script -c ./sftp_test.sh /mylogpath/sftp_test.log

-c 标志表示运行 non-interactive 环境中的命令。