Showing posts with label cron. Show all posts
Showing posts with label cron. Show all posts

Wednesday, March 2, 2011

How to generate consistent "random" numbers

Here is an article that explain a way to get consistent "random" number. The problem appears in virtual environments, like lxc, where hostid is the same. The sample below solves this problem by generating "random" number from a network adapter (eth0) MAC address. So here it is (file func/random_sleep)
#!/bin/bash

# 
# random_sleep shift_max rnd_max
#
# shift_max - a number to generate consistent "random"
# number.
# rnd_max - a random number to add
random_sleep()
{
    shift_max=600
    if [ ! -z $1 ]; then shift_max=$1; fi

    rnd_max=60
    if [ ! -z $2 ]; then rnd_max=$2; fi

    # shift is a number betweem 0 and 600
    shift=`/sbin/ifconfig eth0 | /bin/grep HWaddr | \
        /bin/sed -e 's/.*HWaddr //' | /usr/bin/tr -d ':'`
    shift=$((0x$shift%$shift_max))

    # Sleep a random amount of time + shift
    sleep_time=$(($RANDOM % $rnd_max + $shift))

    echo "Sleeping for $sleep_time seconds..."
    /bin/sleep $sleep_time
}
Here is how you can use it:
#!/bin/bash
. /usr/local/sbin/func/random_sleep
# or
# . `dirname $_`/func/random_sleep

random_sleep 300 60

Monday, February 7, 2011

Email Available Updates for Debian

Before you proceed below please check:
  • Simple apt daily update script, see here.
  • Configure exim4 to send messages by smarthost, no local mail, see here.
We are going enhance daily update script by ability to send a list of new updates to mail.
#!/bin/sh

mailto=debian-updates@dev.local

# Download only; package files are only retrieved, not 
# unpacked or installed.
apt-get -dqq update
apt-get -dyqq upgrade

has_upgrades=$(apt-get -s upgrade | grep ^Inst)
if [ "$has_upgrades" ] ; then
    echo "$has_upgrades" | mail -s \
    "Updates for $(hostname) on $(date +%Y-%m-%d)" \
    $mailto
fi
Alternatively consider using apticron, read more here.

Monday, June 14, 2010

Sync time with external server in Debian

You can synchronize your local computer time with external time servers. You need install ntpdate:
apt-get -y install ntpdate
Try update your time manually:
deby:~# ntpdate pool.ntp.org
12 Jun 00:04:16 ntpdate[1903]: step time server 62.80.187.114 offset -291.468062 sec
Here is a script that syncs the time with external server, adjusts clock drift and finally sets the hardware clock to current system time (file /usr/local/sbin/sync-time).
#!/bin/bash

server=pool.ntp.org

# Sleep a random amount, not greater than 2 minutes
sleep_time=$(($RANDOM % 120))

echo "Sleeping for $sleep_time seconds..."
/bin/sleep $sleep_time

# Sync the time with external server
echo "Sync time with $server."
/usr/sbin/ntpdate -s $server || exit 1

# Adjusts clock drift
/sbin/hwclock --adjust

# Set the hardware clock to current system time
/sbin/hwclock --systohc

Here we are going to schedule a cron job on system startup (file /usr/local/sbin/sync-time, symbolic link from /etc/cron.d/sync-time):
#
# Regular cron job for time synchronization
#
PATH=/usr/local/sbin
HOME=/
LOG=/dev/null

# Every 23 hours, e.g. 2:11, 13:11, etc
11 */23 * * * root test -x /usr/local/sbin/sync-time && sync-time > $LOG
The next time your system restarts it will automatically synchronizes clock with external server. Please note that the system sync the time each time the network interface is up (see ntpdate script in /etc/network/if-up.d/).

Saturday, May 1, 2010

Crontab file syntax

Crontab is a file which contains the schedule of cron entries to be run and at specified times.
*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)
Examples:
# 00:30 on 1st of Jan, June & Dec
30    0   1        1,6,12  *
# 8.00 PM every weekday (Mon-Fri) only in Oct 
0     20  *        10      1-5  
# Midnight on 1st ,10th & 15th of month
0     0   1,10,15  *       *
# 12.05,12.10 every Monday & on 10th of every month 
5,10  0   10       *       1  
# Every 2 hours, at 2am, 4am, 6am, and so on
0     */2 *        *       *
You can simply drop your cron schedule file into /etc/cron.d/. Read more about cron here.

Friday, April 30, 2010

Simple apt daily update script

Here is a simple apt update script (file /usr/local/sbin/apt-update):
#!/bin/sh

# Download only; package files are only retrieved, not unpacked or
# installed.
apt-get -dqq update
apt-get -dyqq upgrade
Make this file executable:
chmod +x /usr/local/sbin/apt-update
Run it by cron per schedule (file /usr/local/etc/cron.d/apt-update):
#
# Regular cron jobs for apt-update
#
SHELL=/bin/sh
PATH=/usr/local/sbin:/bin:/usr/bin
LOG=/dev/null

# Run on Sun, Tue, Thu at 2:05 AM
# m h dom mon dow user  command
05 2 * * 0,2,4 root test -x /usr/local/sbin/apt-update && apt-update > $LOG
Let cron know about our scheduled apt-update:
ln -s /usr/local/etc/cron.d/apt-update /etc/cron.d/cron-apt-update

Friday, April 23, 2010

Simple tar backup script

Here is a simple backup script (/usr/local/sbin/backup):
#!/bin/bash
#
# creates backups of essential files
#

BACKUPDIR=/var/backups
KEEPDAYS=7

DATA="/root /home /srv \
/boot/grub/menu.lst \
--exclude=$BACKUPDIR"

CONFIG="/etc /var /usr/local \
--exclude=/var/cache/apt/archives \
--exclude=/var/cache/man \
--exclude=/var/run \
--exclude=/var/tmp \
--exclude=/var/lock \
--exclude=$BACKUPDIR \
--exclude=*.gz"

#
# implementation details
#

DATE=`date +%Y-%m-%d-%H-%M`
COMPUTER=`hostname`

# Remove files older than KEEPDAYS days
find "$BACKUPDIR/" -name $COMPUTER-*.tgz \
-type f -mtime +$KEEPDAYS -delete

# Backup data

BACKUPFILE="$BACKUPDIR/$COMPUTER-data-$DATE.tgz"
tar cfz $BACKUPFILE $DATA \
--ignore-failed-read 2> /dev/null

chmod o-o $BACKUPFILE
chgrp adm $BACKUPFILE

# Backup configs

BACKUPFILE="$BACKUPDIR/$COMPUTER-config-$DATE.tgz"
tar cfz $BACKUPFILE $CONFIG \
--ignore-failed-read 2> /dev/null

chmod o-o $BACKUPFILE
chgrp adm $BACKUPFILE
Run it by cron per schedule (file /usr/local/sbin/cron-backup):
#
# Regular cron jobs for backup
#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
HOME=/
LOG=/dev/null

# Every day at 21:15
15 21 * * * root test -x /usr/local/sbin/backup && backup >> $LOG
Let cron know about our scheduled backup:
ln -s /usr/local/sbin/cron-backup /etc/cron.d/cron-backup
The backup files are in $BACKUPDIR (/var/backups):
deby:~# ls -lh /var/backups/
total 38M
-rw-r----- 1 root adm   19M 2010-04-23 23:30 deby-config-2010-04-23-23-30.tgz
-rw-r----- 1 root adm  4.3K 2010-04-23 23:30 deby-data-2010-04-23-23-30.tgz
...