Question

I am trying to capture output from an install script (that uses scp) and log it. However, I am not getting everything that scp is printing out, namely, the progress bar.

screen output:

Copying /user2/cdb/builds/tmp/uat/myfiles/* to server /users/myfiles as cdb

cdb@server's password: myfile 100% |*****************************| 2503 00:00

log output:

Copying /user2/cdb/builds/tmp/uat/myfiles/* to server /users/myfiles as cdb

I'd really like to know that my file got there. Here's what I am trying now to no avail:

myscript.sh 2>&1 | tee mylogfile.log

Does anyone have a good way to capture scp output and log it?

Thanks.

Was it helpful?

Solution

It looks like your just missing whether the scp was succesful or not from the log.

I'm guessing the scroll bar doesn't print to stdout and uses ncurses or some other kind of TUI?

You could just look at the return value of scp to see whether it was successful. Like

scp myfile user@host.com:. && echo success!

man scp says

scp exits with 0 on success or >0 if an error occurred.

OTHER TIPS

scp prints its progress bar to the terminal using control codes. It will detect if you redirect output and thus omit the progress bar.

You can get around that by tricking scp into thinking it runs in a terminal using the "script" command which is installed on most distros by default:

script -q -c "scp server:/file /tmp/" > /tmp/test.txt

The content of test.txt will be:

file    0%    0     0.0KB/s   --:-- ETA
file   18%   11MB  11.2MB/s   00:04 ETA
file   36%   22MB  11.2MB/s   00:03 ETA
file   54%   34MB  11.2MB/s   00:02 ETA
file   73%   45MB  11.2MB/s   00:01 ETA
file   91%   56MB  11.2MB/s   00:00 ETA
file  100%   61MB  10.2MB/s   00:06

...which is probably what you want.

I stumbled over this problem while redirecting the output of an interactive script into a log file. Not having the results in the log wasn't a problem as you can always evaluate exit codes. But I really wanted the interactive user to see the progress bar. This answer solves both problems.

Maybe you can use 'script' to log the terminal session.

scp myfile user@host.com:. && echo success! 

is very helpful but to write the message to a log file I changed it like this

scp myfile user@host.com:. && echo myfile successfully copied! >> logfile 2>&1

and this will write "myfile successfully copied!" message to the log file.

Try:

scp server:/file /tmp/ > /dev/tty

I can't comment yet :( so I'll add an update here...

@Martin had the best solution for me although if your scp command is midway through your script then it's output may appear after commands that actually ran afterwards.

I think that is because script must run the command in a subshell but I am yet to test.

EDIT: it does indeed spawn a shell so if you need things to run (and indeed fail) in a sequential manner (like in a build script) then you would have to add some logic around the use of the script command.

i.e.

script -q -c "your command" && sleep 1

or something similar so that your parent shell waits for the child shell to finish before moving on.

yes i recently was trying to get output within a php script from proc_open() i lost a quiet a time trying to get output :-) but its a bit late here and i then reading this post here made me realize that i dont really need this junky output to my script

just the exit code will do the job :-)

$exit_code = proc_close($process);

$ grep -r "Error" xyz.out > abc.txt

Here in the above command I am storing output into file abc.txt.

This grep command is for searching text containg Error in file xyz.out and storing the output in abc.txt without displaying on console.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top