1#!/bin/bash 2 3# Positive tests for cron, that means these tests have to pass 4 5iam=`whoami` 6 7tvar=${MACHTYPE%-*} 8tvar=${tvar#*-} 9 10if [ "$tvar" = "redhat" -o "$tvar" = "redhat-linux" ] 11then 12 CRON_ALLOW="/etc/cron.allow" 13else 14 CRON_ALLOW="/var/spool/cron/allow" 15fi 16 17 18if [ $iam = "root" ]; then 19 if [ $# -lt 1 ] ; then 20 echo Either do not run this script as root or start it like 21 echo " $0 <user>" 22 exit 1 23 fi 24 25 mv $CRON_ALLOW $CRON_ALLOW.old &> /dev/null 26 su $1 -c "$0 $*" 27 RC=$? 28 mv $CRON_ALLOW.old $CRON_ALLOW &> /dev/null 29 exit $RC 30fi 31 32function restorecrontab () { 33 test -e /tmp/crontab-save-$iam && \ 34 crontab /tmp/crontab-save-$iam && \ 35 rm -f /tmp/crontab-save-$iam && \ 36 echo restored old crontab 37} 38 39echo Running as user $iam... 40 41# Save current users crontab 42 43test -e /tmp/crontab-save-$iam && rm -f /tmp/crontab-save-$iam 44 45if [ "0" -lt `crontab -l 2>/dev/null | wc -l` ]; then 46 47 echo 'crontab of this user exists -> creating backup' 48 crontab -l | grep '^[^#]' > /tmp/crontab-save-$iam 49fi 50 51 52# Do tests 53 54# 1. Add new job 55 56rm -rf /tmp/crontest &> /dev/null 57mkdir -p /tmp/crontest 58 59cat > /tmp/crontest/testjob_cron01 << EOF 60echo Testjob running 61date 62EOF 63 64chmod 755 /tmp/crontest/testjob_cron01 65 66crontab - << EOF 67`date '+%M' | awk '{ print ($1+2)%60 " * * * * " 68}'` /tmp/crontest/testjob_cron01 >> /tmp/crontest/output_cron01 2>&1 69EOF 70 71rc=$? 72 73if [ $rc = "1" ]; then 74 echo Error while adding crontab for user $iam 75 restorecrontab 76 exit 1 77fi 78 79echo new job added successfully 80 81# 2. Wait for execution of job 82 83echo 'sleeping for 130 seconds...' 84sleep 130 85 86rc=1 87test -e /tmp/crontest/output_cron01 && rc=0 88 89if [ $rc = "1" ]; then 90 echo Job has not been executed 91 restorecrontab 92 exit 1 93fi 94 95grep "Testjob running" /tmp/crontest/output_cron01 96rc=$? 97if [ $rc = "1" ]; then 98 echo Job has not produced valid output 99 restorecrontab 100fi 101 102echo 'job has been executed :-)' 103echo "testjob's output:" 104echo 105 106rm -rf /tmp/crontest 107 108# 3. Delete crontab 109 110crontab -r 111 112echo removed crontab 113 114# Restore old crontab file 115 116restorecrontab 117 118exit $rc 119