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

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 />
#!/bin/bash</p>
<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>
<p>trap term_dd SIGINT SIGTERM</p>
<p>args=("$@")</p>
<p>if [ ${#args} -lt 2 ]; then<br />
	echo "usage: $0 infile outfile [other options]"<br />
	exit<br />
fi</p>
<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.

Comments

Add new comment

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

About the Author...

Slides from LISA 2019 Linux systems troubleshooting #LISA2019 https://t.co/D4dMKflK6R Tue Oct 29 05:59:30 +0000 2019

https://t.co/AGeihMALAv configuring grub2 with EFI Fri Sep 13 05:20:01 +0000 2019

I published a Thing on @thingiverse! https://t.co/IYpRyEb7Hz #thingalert Tue Jul 23 19:27:57 +0000 2019

Nokogiri install on MacOSX https://t.co/v3An0miW9L Fri Jul 12 15:06:49 +0000 2019

HTML email with plain mailer plugin on Jenkins https://t.co/Z6FSDMDjy8 Thu Jul 11 21:07:25 +0000 2019