Article Listing 1 Listing 2 Listing 3 jun2007.tar

Listing 2 start_cron

#!/bin/sh
# Listing 2: start_cron

. /etc/setenv_path

users=$@

if [ -z "$users" ]
then
    while read user
    do
        users="$users $user"
    done
fi

CRON_ALLOW=/etc/cron.d/cron.allow
SORTINGHAT_DIR=/opt/SortingHat

for user in $users
do
    if grep -w $user $CRON_ALLOW
    then
        echo "Starting $user's cron..."
        file=/var/spool/cron/crontabs/$user
        current_file=$SORTINGHAT_DIR/current$file
        backup_file=$SORTINGHAT_DIR/backup$file
        if [ ! -f $file ]
        then
            if [ -f $backup_file ]
            then
                chown $user $backup_file
                chmod +r $backup_file
                su $user -c "crontab $backup_file"
                echo "The $user cron has been started."
                echo "A backup copy was restored."
                rm -f $backup_file
            else
                $SORTINGHAT_DIR/bin/SortingHat -f $file
                if [ -f $current_file ]
                then
                    chown $user $current_file
                    chmod +r $current_file
                    su $user -c "crontab $current_file"
                    echo "The $user cron has been started."
                else
                    echo "Could not find a current version of the $user crontab file."
                fi
            fi
        else
            echo "The $user crontab file already exists."
        fi
    else
        echo "$user is not in $CRON_ALLOW"
    fi
done