LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Catching stderr from dd over ssh (https://www.linuxquestions.org/questions/linux-general-1/catching-stderr-from-dd-over-ssh-835714/)

bcnx 10-01-2010 05:58 PM

Catching stderr from dd over ssh
 
Hi,

this might be an interesting one for the bash scripting gurus. I seem to break my teeth on it.

The mission:
- do a dd over ssh to trasnfer an image to another host
- capture the dd PID on the other side
- send a USR1 kill signal to it
- capture that output on the original host

It goes wrong on the last part. This is what I have before that step:


dd if=image.gz | gzip -d | ssh host2 "dd of=/dev/vg1/lv1" &
PID=`ssh host2 ps aux |grep dd|grep lx05|awk '{ print $2 }'`

when I do "ssh host2 kill -USR1 $PID

I get nice outputs to the screen.

When I replace the first line with:

dd if=image.gz | gzip -d | ssh host2 "dd of=/dev/vg1/l01 2>/tmp/output.txt" &

the dd command seemd to die. I suspect a problem with the pipe, since this does work when executing locally on a host without piping.

Anyone any clue on how I can redirect the dd output to a file or even better to the originating script ?

thx !!!


B.

neonsignal 10-01-2010 07:39 PM

The output already comes back to the originating script - for example:
Code:

echo "abc" | ssh host2 "cat -B" 2>/tmp/error.txt
will put the "invalid option" error message into error.txt on the local machine.

Incidentally, your replacement line has /dev/vg1/l01 whereas the original had /dev/vg1/lv1. Was this a typo?

bcnx 10-02-2010 03:37 AM

Yes, this was a type, sorry.

Huh, this is so weird, this works right of the bat now while I'm sure I tried that before. Must have made some error somewhere ... :-s

Do you think there is any way to capture this in a variable without redirecting to a file? I'm going to try and just capture the first word of the last line, because that shows the progress ...

thx!!!


B.

neonsignal 10-02-2010 04:59 AM

Quote:

Originally Posted by bcnx (Post 4115479)
Do you think there is any way to capture this in a variable without redirecting to a file?

Yeah, though it starts to get a bit messy, so you are perhaps better off having a temporary file than following something like this example:
Code:

ERROR=$( { echo "abc" | ssh host2 "cat -B" ; } 2>&1 | tail -1 )
This takes the pipeline in the braces, redirects all of its stdout and stderr together through tail to strip off all but the last line, and assigns it to $ERROR. You can throw away the stdout inside the braces by redirecting to /dev/null if you wanted. The semicolon is required.

bcnx 10-02-2010 04:08 PM

Hi Neonsignal,

thank you for that. I tried your suggestion, but that seemed to go wrong somewhere so I decided to stay with the idea of redirecting to a temporary file. That too went wrong, but that seemed to be because sometimes something goes wrong with capturing the output of dd over ssh and the relevant variable appeared empty as a result. I fixed this by examining this variable for being empty in the loop and then assigning the previous empty for it again. Since I'm checking every 5 seconds for progress on a +10 minute process, this hardly is noticed.

thank again!

B.


All times are GMT -5. The time now is 07:13 AM.