Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / testcases / commands / cron / cron_pos_tests.sh
blobacc08cb312d3ad1cbe4272548a0106380717d319
1 #!/bin/bash
3 # Positive tests for cron, that means these tests have to pass
5 iam=`whoami`
7 tvar=${MACHTYPE%-*}
8 tvar=${tvar#*-}
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"
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
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
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
39 echo Running as user $iam...
41 # Save current users crontab
43 test -e /tmp/crontab-save-$iam && rm -f /tmp/crontab-save-$iam
45 if [ "0" -lt `crontab -l 2>/dev/null | wc -l` ]; then
47 echo 'crontab of this user exists -> creating backup'
48 crontab -l | grep '^[^#]' > /tmp/crontab-save-$iam
52 # Do tests
54 # 1. Add new job
56 rm -rf /tmp/crontest &> /dev/null
57 mkdir -p /tmp/crontest
59 cat > /tmp/crontest/testjob_cron01 << EOF
60 echo Testjob running
61 date
62 EOF
64 chmod 755 /tmp/crontest/testjob_cron01
66 crontab - << EOF
67 `date '+%M' | awk '{ print ($1+2)%60 " * * * * "
68 }'` /tmp/crontest/testjob_cron01 >> /tmp/crontest/output_cron01 2>&1
69 EOF
71 rc=$?
73 if [ $rc = "1" ]; then
74 echo Error while adding crontab for user $iam
75 restorecrontab
76 exit 1
79 echo new job added successfully
81 # 2. Wait for execution of job
83 echo 'sleeping for 130 seconds...'
84 sleep 130
86 rc=1
87 test -e /tmp/crontest/output_cron01 && rc=0
89 if [ $rc = "1" ]; then
90 echo Job has not been executed
91 restorecrontab
92 exit 1
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
102 echo 'job has been executed :-)'
103 echo "testjob's output:"
104 echo
106 rm -rf /tmp/crontest
108 # 3. Delete crontab
110 crontab -r
112 echo removed crontab
114 # Restore old crontab file
116 restorecrontab
118 exit $rc