Archive

Posts Tagged ‘script’

An ugly fix

July 26th, 2009 No comments

My home server seems to have developed a problem where its internal-facing network card “jams up”. It still keeps its IP address and everything looks normal, but in actual fact no traffic passes through it, cutting all my LAN hosts off from the Internet.

Restarting iptables, network services or anything else doesn’t help. The only cure I’ve found is to reboot the whole box.

This is OK if I’m using the computer at the time, but a pain if it breaks overnight or while I’m out, as things like my folding@home client, Vista Media Centre TV listings and overnight BitTorrent downloads need Internet access.

So I wrote this bash script to periodically check if it’s broken, and reboot if it is. The only gotcha is that it tests if the interface is working by pinging another LAN host. This is by no means a concrete test!

#!/bin/bash
HOST=192.168.0.10
if ! ping -c 1 -w 5 "$HOST" &>/dev/null ; then
logger ZEUS REBOOT
/sbin/init 6
fi

Categories: Linux, Networking Tags: , , , ,

Batch conversion of images on Linux

June 27th, 2009 No comments

I recently had a need to convert 3,000 scanned TIFF images to a more sensible format for distribution, such as JPEG. I’ve been using GIMP to edit the individual photos but sadly it doesn’t have a batch format conversion tool.

So I wrote my own little script in bash. You will need to install ImageMagick (a commandline image editor) on your box first, but this is bundled with most distributions and be installed from a repository.

Things to be aware of when using this script:

  • It does not recurse into subdirectories – you have to place the script in the same directory that the TIFFs are in
  • At the time of writing, ImageMagick is single-threaded and can only use one core of a multicore CPU. If you want to use N cores, you have to split the photos into N directories and run N copies of the script. However be aware that, in my case at least, each TIFF was around 45MB and took between 1 and 1.5 seconds to convert. This means the disk is reading at maybe 40MB/s, so running two threads you will quickly hit the limit of your disk.
  • You can change the compression of the JPG by altering the value 85 in the script below.
  • You can also convert your TIFFs to other formats. See man convert for details.

Finally, the code you’ve all been waiting for. Feel free to use, edit, distribute this anywhere you please.

#!/bin/bash

total=`ls *.tif | wc -l`
count=0
ls *.tif | while read i
do
file=`basename "$i" .tif`
echo $file
convert "$file.tif" -quality 85 "$file.jpg"
count=`expr $count + 1`
percent=$(($count / $total))
echo $percent% completed, done $count of $total
done
echo All done!