Home | Linux



Using the 'watch' command

Here's a useful command to keep an eye on what's going on with sendmail. You can see all the current sendmail connections, followed by the last 10 lines of the sendmail log, updated every second.

If you have a lot of sendmail connections, it will scroll off your screen and probably not be so useful. But on my moderate traffic server, with 10-15 connections at all times, it's just right.

watch -n 1 'ps afx --width=300|grep sendmail; \
echo;echo;tail -10 /var/log/maillog'

The \ is just for display purposes. You can put the whole command on one line if you want to.

Using 'for loops' and 'seq' to handle files

To repeat a command or series of commands in the shell, you can use a for loop, just like you would in most any progamming language. And the seq command will generate a sequence of numbers for you. This makes processing image files, for example, fairly simple.

Let's say you have files named "image001.jpg" through "image275.jpg" that you want to run through some sort of image alteration program, img_foo. But img_foo isn't smart enough to handle multiple files; it can only handle a single filename as a runtime option. To process every file, you can do

for f in `seq -w 1 275`; do img_foo image$f.jpg; done

The -w switch for seq will pad the digits generated (1 to 275) with leading zeros to keep all numbers the same width. The loop variable $f gets assigned each seq-generated number in turn, so each pass through the loop gives a new filename being passed to img_foo.

Try using seq alone, with and without the -w switch, to see what it does. And remember, man is your friend -- man seq will give you all the info you need.

 

If you find this page useful, please consider making a secure donation:

Creative Commons Attribution Share-Alike License logo
This work is copyright © 2002-2009 Bruce Timberlake and is licensed under a Creative Commons Attribution Share-Alike License. It may be copied, modified, and distributed freely as long as it attributes the original author by name ("Bruce Timberlake") and URL ("http://brucetimberlake.com/") and maintains the original license. See the license for details.