How to best capture and log scp output?

前端 未结 9 1180
栀梦
栀梦 2020-12-31 01:55

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. <

相关标签:
9条回答
  • 2020-12-31 02:29

    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);

    0 讨论(0)
  • 2020-12-31 02:33

    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.

    0 讨论(0)
  • 2020-12-31 02:35

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

    0 讨论(0)
  • 2020-12-31 02:37

    None of the answers here worked for me, I needed to recursively copy large directory with lot of files over long geo distance, so I wanted to log the progress (&& echo success! was by far not enough).

    What I finally engineered and somehow worked was:

    scp -vrC root@host:/path/to/directory . 2> copy.log &
    

    With -v doing the trick of verbose logging (-C allows compression and -r recursion).

    Grepping the logfile

    grep file copy.log | wc -l
    

    allowed me to see the number of files copied so far.

    0 讨论(0)
  • 2020-12-31 02:44
    $ 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.

    0 讨论(0)
  • 2020-12-31 02:45

    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.
    
    0 讨论(0)
提交回复
热议问题