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