The watch & pgrep Commands
Today I found myself using watch to debug a hairy ConTeXt problem. ConTeXt includes a script called texexec that wraps pdfetex and the various other TeX engines. Older version of ConTeXt could find my project local font map, but newer versions could not. Both used the same version of pdfetex, but newer versions include a Ruby version of texexec instead of the original Perl script. Obviously the two files were very different, so I couldn't just diff them. A few minutes browsing both scripts didn't turn anything up, so I decided to just run them and see what options were being passed to pdfetex. Which brings us to this pretty little command:
watch 'cat -A /proc/$(pgrep pdfetex)/cmdline'
The watch command will periodically re-run a command and display its output. I was first introduced to it as a useful way to monitor a software RAID array as it is rebuilding:
watch cat /prod/mdstat
Since then, I've periodically found myself using it to monitor some other process. For example, monitoring the progress of a large file as it moves from one filesystem to another (note the use of a single ampersand):
mv /media/usbdisk1/foo.dat /media/usbdisk2 & watch df -hOr monitoring who is currently logged into the system:
watch w
Eagle eyed readers may have noticed the use of single quotes around the cat command in my original example:
watch 'cat -A /proc/$(pgrep pdfetex)/cmdline'Single quotes were necessary because I wanted watch to run pgrep every time it ran cat.
So what does pgrep do? As you can probably guess from the previous example, it returns the PID of every process with a certain name. In this case, I was taking advantage of the fact there would be only one pdfetex running at a time.