Delay a script by variable time

By jbayer - Last updated: Monday, January 31, 2011 - Save & Share - Leave a Comment

When managing many systems in a server farm or a virtual environment, quite often the same script needs to be run on all the systems.  However, if the script happens to put a severe load on a common resource such as a storage device, the possibility exists that the storage device can be overwhelmed and all the scripts will run slowly.  One way to avoid this is to stagger the running of the scripts on all the systems.

If you are trying to keep the systems identical and avoid customizing scripts or configuration files for each server, a simple way to delay is to use something on the system which is unique, such as the IP address.

The attached script does just that.  It has reasonable defaults, but you can specify which octet to use to when calculating a delay period, a multipler of that octet, a network to execute on, and which network interface to use.  If the network interface is not specified then it will find the first ethernet interface and use that.  The usage is as follows:

usage: [ options ] script [ script options ]

 -o #        Octet to use (default to 4)
 -m #        Multiplier to use (default to 1)
 -n network  Only execute on this network (default to 10.11)
 -e iface    Network interface to use

As an example, we use this to stagger the running of the updatedb script.  We need updatedb to run hourly, but don’t want 10 systems hitting the storage unit at the same time.  So the following entry is in /etc/crontab:

0 * * * *          root    /usr/local/sbin/ -o 4 -m 3 -n 10.11  /usr/bin/updatedb

  scriptdelay.tar.gz (1.8 KiB, 326 hits)

Posted in Administration, Bash • • Top Of Page

Write a comment