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

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

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.


Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

About the Author...