You are heredd'ing and printing progress (a lame script I wrote for something I do a lot...)

dd'ing and printing progress (a lame script I wrote for something I do a lot...)


By thomas - Posted on 13 June 2012

I often use dd to copy large files or make images from hard drives. It's annoying to watch something without any progress indicator, so I use the little known kill switch on dd.

From the man page:

Sending a USR1 signal to a running ‘dd’ process makes it print I/O statistics to standard error and then resume copying.

$ dd if=/dev/zero of=/dev/null& pid=$!
$ kill -USR1 $pid; sleep 1; kill $pid

I'll often make a for loop and keep killing dd to get the stats, I decided to make a little script to do the whole thing in one, making it even lazier...

#!/bin/bash
 
# run dd and kill with USR1 every 5 seconds.
 
term_dd() {
  echo "killing dd"
  kill $dd
  exit 1
}
 
trap term_dd SIGINT SIGTERM
 
args=("$@")
 
if [ ${#args} -lt 2 ]; then
	echo "usage: $0 infile outfile [other options]"
	exit
fi
 
tmp=$(mktemp /tmp/ddk.XXXXXXXX)
 
echo dd if=$1 of=$2 ${args[@]:2}
dd if=$1 of=$2 ${args[@]:2} >$tmp 2>&1 &
dd=$!
echo "dd if=$1 of=$2 ${args[@]:2} $dd"
sleep 1
while /bin/true
do
    if [ -d /proc/$dd ];
    then
      kill -USR1 $dd
    else
      echo "no dd $dd"
      break
    fi
    clear
    echo "dd if=$1 of=$2 ${args[@]:2}"
    tail -3 $tmp
    sleep 1
done
rm $tmp
exit

The only caveat here is that the sleep right before starting the while loop appears to be necessary, without it, the whole thing won't work sometimes, I can't quite figure out why...The $! doesn't seem to be populated quick enough and the script dies at that point since the first kill files to find the /proc/$! directory.

Trackback URL for this post:

http://ramblings.narrabilis.com/trackback/311
Tags

http://dcfldd.sourceforge.net/

Great script, I do a quick for loop as mentioned, but your script is much cleaner. Will add to my arsenal.

A number of years ago I ran into dcfldd and have been using it when I can. It has a builtin status bar, but the best is the hash on the fly function. I found that I was doing a cryptographic hash (md5, then sha5, then sha256, etc) on both the source and then the destination after the dd was finished. Talk about a waste of time.

Dcfldd does it during the transfer. It can even do it in specified window sizes. Drastically reduces the verification time.

It's saved me more than once on bad drives, bad betworks, or solar flares. Might be one more tool for the belt.

As always, thanks!

-ep

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, </bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <latex>, </latex>, <sql>, </sql>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.