首页 > 解决方案 > time + sftp 命令脚本:如何正确地将输出重定向到日志文件

问题描述

我构建了一个脚本来自动化日常的 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&

但它没有按预期工作。只保存像“获取 /myremotepath/myfile2.txt.gz 到 /mylocalpath/myfile2.txt.gz”这样的行。不是时间命令输出。不是其他 sftp 线路。

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

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

我怎样才能做到这一点?

标签: linuxbashshellubuntusftp

解决方案


脚本可能是这里的一个选项,所以:

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

-c 标志表示在非交互式环境中运行命令。


推荐阅读