Watching Jobs with watch

Use watch to repeatedly run any command, and show you the results

If you have ever had a long-running background process, chances are you are used to repeatedly checking to see if the command has finished using ps and grep:

ray@linux:~$ ps ax |grep tar

                 10303 ? S 0:00 bash -c cd /; tar cf - home

                 10304 ? S 0:42 tar cf - home

ray@linux:~$ ps ax |grep tar

                 10303 ? S 0:00 bash -c cd /; tar cf - home

                 10304 ? S 0:43 tar cf - home

Or maybe you're ftping a file, and forgot the hash command (and for some reason, aren't using ncftp or scp or wget or curl), so you have no idea how far along your transfer is. Naturally, you login again and take a look at the file with ls:

ray@linux:~$ ls -l xfree86.tgz

                   -rw-r--r-- 1 rob users 12741812 Jun 13 2001 xfree86.tgz

ray@linux:~$ ls -l xfree86.tgz

                     -rw-r--r-- 1 rob users 12744523 Jun 13 2001 xfree86.tgz

Any time you find yourself running a command over and over again, try the watch command. It will repeatedly cycle over any command you give it, and whatever interval you'd like (defaulting to 2 seconds). It clears the screen on each pass, making a nice, easy to read display.

ray@linux:~$ watch 'ps ax |grep tar'

Every 2s: ps ax |grep tar|grep -v pts/0 Fri Sep 6 00:22:01 2002

               10303 ? S 0:00 bash -c cd /; tar cf - home

               10304 ? S 0:42 tar cf - home

You'll only need to enclose the command in single quotes if you're using pipes (or other special characters that you don't want interpolated before watch runs). To specify a different time interval, try the -n switch:

ray@linux:~$ watch -n1 ls -l xfree86.tgz

Every 1s: ls -l xfree86.tgz Fri Sep 6 00:31:41 2002

-rw-r--r-- 1 rob users 12756042 Jun 13 2001 xfree86.tgz

It will even highlight the differences on each pass, making changes leap out in reverse type. Try the -d switch for that (it's especially nice for watching the output of a netstat -a | grep ESTAB, to watch network connections come and go. Think of watch as your obsessive-compulsive little buddy. It obsesses at your slightest whim, so you don't have to. To exit, hit ^C.

0 comments:

Post a Comment