Tuesday, October 26, 2021

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

Issue

I built a script to automatize a daily ftp download process. To make it simpler, something like the following:

#!/usr/bin/env bash
time sshpass -p "mypass" sftp [email protected]:/myremotepath/myfile.txt.gz /mylocalpath/myfile.txt.gz 
time sshpass -p "mypass" sftp [email protected]:/myremotepath/myfile2.txt.gz /mylocalpath/myfile2.txt.gz 

I execute the script like the following to redirect the output lines to a log file:

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

But it is not working as expected. Just lines like "Fetching /myremotepath/myfile2.txt.gz to /mylocalpath/myfile2.txt.gz" are saved. Not the time command output. Not the others sftp lines.

I would like to save the output exactly as I see it if I execute the script manually without the " > /mylogpath/sftp_test.log 2>1&" part.

I also tried with the tee command without success.

How can I do that?


Solution

Script may be an option here and so:

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

The -c flag signifies to run a command in a non-interactive environment.



Answered By - Raman Sailopal