Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / testcases / commands / cron / cron_tests.sh
blob1851fe6d8886f919ee1c22aeee2edd8413b3fa44
1 #!/bin/sh
3 ################################################################################
4 ## ##
5 ## Copyright (c) International Business Machines Corp., 2001 ##
6 ## ##
7 ## This program is free software; you can redistribute it and#or modify ##
8 ## it under the terms of the GNU General Public License as published by ##
9 ## the Free Software Foundation; either version 2 of the License, or ##
10 ## (at your option) any later version. ##
11 ## ##
12 ## This program is distributed in the hope that it will be useful, but ##
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
14 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ##
15 ## for more details. ##
16 ## ##
17 ## You should have received a copy of the GNU General Public License ##
18 ## along with this program; if not, write to the Free Software ##
19 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ##
20 ## ##
21 ## ##
22 ################################################################################
24 # File: cron_tests.sh
26 # Description: This testcase tests if crontab <filename> installs the cronjob
27 # and cron schedules the job correctly. The job is set such that it will run
28 # forever every minute of the day.
29 # The cronjob runs a program that will print a string followed by the current
30 # date and time. Five samples are taken inorder to verify if cron job is run
31 # every minute. It is not practical to check if it works for the remaining
32 # fields of the crontab file also.
34 # Author: Manoj Iyer manjo@mail.utexas.edu
36 # History:
37 # Dec - 19 - 2002 - Created.
38 # Dec - 20 - 2002 - Correted Test #3, grep for the filename of cronjob
39 # after executing crontab -l.
40 # - Fixed bug in #3, test was not installing the cronjob.
41 # - Added more informational messages TINFO.
42 # - Changed permissions to this file to 'x'
44 export TST_TOTAL=3
46 if [ -z "$LTPTMP" -a -z "$TMPBASE" ]
47 then
48 LTPTMP=/tmp
49 else
50 LTPTMP=$TMPBASE
53 LTPBIN=$LTPTOOLS
55 # Set return code RC variable to 0, it will be set with a non-zero return code
56 # in case of error. Set TFAILCNT to 0, increment if there occures a failure.
58 LOCTMP=${PWD}/tmp
59 TFAILCNT=0
60 RC=0
62 # Test #1
63 # Test if crontab <filename> installs the crontab file and cron schedules the
64 # job correctly.
66 export TCID=cron01
67 export TST_COUNT=1
69 $LTPBIN/tst_resm TINFO "Test #1: crontab <filename> installs the crontab file"
70 $LTPBIN/tst_resm TINFO "Test #1: cron schedules the job listed in crontab file."
72 # create the cron job. The job is to run the program tst1_cronprg.sh
73 # every minute, every hour, every day, every month, any weekday.
75 cat > $LTPTMP/tst1_cronjob.cron <<EOF
76 * * * * * $LTPTMP/tst1_cronprg.sh
77 EOF
79 # Create the program that will be run by the cronjob. This program will print a
80 # "Hello Hell" string and date time information.
82 cat > $LTPTMP/tst1_cronprg.sh <<EOF
83 #! /bin/sh
85 DATE=\`LANG= date\`
86 echo "Hello Hell today is \$DATE " > $LTPTMP/tst1_cron.out 2>&1
87 exit 0
88 EOF
90 chmod +x $LTPTMP/tst1_cronprg.sh
92 # install the cronjob, crontab <filename> does that. Sleep for 10s and the
93 # check the /var/log/messages to see if there is a record of any crontab
94 # activity.
96 $LTPBIN/tst_resm TINFO "Test #1: Installing cron job ... "
97 crontab $LTPTMP/tst1_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1
98 RC=$?
100 if [ $RC -ne 0 ]
101 then
102 $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \
103 "Test #1: crontab Broke while installing cronjob. Reason:"
104 TFAILCNT=$(( $TFAILCNT+1 ))
105 else
106 $LTPBIN/tst_resm TINFO "Test #1: Cronjob installed successfully"
109 sleep 10s
111 tail -n 10 /var/log/messages | grep crontab | grep REPLACE \
112 > $LTPTMP/cron_tst2n1.out 2>&1
113 RC=$?
114 #####
115 # Some implementations log cron info to /var/log/cron instead...
116 #####
117 if [ "$RC" -ne 0 -a -f /var/log/cron ]; then
118 $LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: Trying altenate log..."
119 tail -n 10 /var/log/cron | grep crontab | grep REPLACE \
120 > $LTPTMP/cron_tst2n1.out 2>&1
121 RC=$?
123 if [ $RC -ne 0 ]
124 then
125 $LTPBIN/tst_resm TFAIL \
126 "Test #1: crontab activity not recorded in /var/log/messages."
127 TFAILCNT=$(( $TFAILCNT+1 ))
128 else
129 $LTPBIN/tst_resm TINFO \
130 "Test #1: cron activity logged in /var/log/messages"
133 # just wait a random time for the cron to kickoff the cronjob.
134 #####
135 # Sleep enough to get _just past_ the start of the next minute --
136 # like 2 or 3 seconds past... since the loop below sleeps for 62
137 # seconds, we should start this 5-iteration loop closely following
138 # the start of a minute...
139 #####
140 sleep 1m # allows cron to run once
141 XS=$(expr 60 - $(date | awk '{print $4}' | cut -f3 -d:))
142 [ "$XS" -ne 0 ] && sleep ${XS}s # sleep to the _next_ minute
143 sleep 3 # ... for good measure...
145 # The program executed by the cron job tst1_cronprg.sh will record the date
146 # and time in a file tst1_cron.out. Extract the minute recorded by the program
147 # into TS_MIN1 sleep for 1m 10s so that the cron will update this file after
148 # 1m, extract TS_MIN2 and check if the minute recorded has advanced by 1. Take
149 # 5 such samples, if any one of the fail, flag a failure.
151 LOOP_CNTR=5
152 TS_MIN1=0
153 FAILCNT=0
155 while [ $LOOP_CNTR -ne 0 ]
157 TS_MIN1=$(awk '{print $8}' $LTPTMP/tst1_cron.out |
158 awk -F: '{printf("%d", $2);}')
160 # wait for the cronjob to update the tst1_cron.out file.
161 sleep 1m 2s
163 # check the time recorded in the tst1_cron.out file,
164 # this should be 1 minute ahead of what was recored earlier.
166 TS_MIN2=$(awk '{print $8}' $LTPTMP/tst1_cron.out |
167 awk -F: '{printf("%d", $2);}')
169 if [ "x${TS_MIN1}" = "x" ] || [ "x${TS_MIN2}" = "x" ]
170 then
171 $LTPBIN/tst_resm TFAIL \
172 "Test #1: Problem with $LTPTMP/tst1_cron.out file "
173 $LTPBIN/tst_resm TFAIL \
174 "Test #1: Cause: TS_MIN1= $TS_MIN1; TS_MIN2= $TS_MIN2"
175 FAILCNT=$(( $FAILCNT+1 ))
176 break;
179 if [ $TS_MIN1 -eq 59 ]
180 then
181 TS_MIN1=0
182 else
183 TS_MIN1=$(( $TS_MIN1+1 ))
186 if [ $TS_MIN2 -ne $TS_MIN1 ]
187 then
188 # if the value of the minute field did not advance by 1
189 # flag as failure.
190 FAILCNT=$(( $FAILCNT+1 ))
191 echo " Expected $TS_MIN1; Received $TS_MIN2" \
192 > $LTPTMP/tst1_cron.log
193 $LTPBIN/tst_res TFAIL $LTPTMP/tst1_cron.log \
194 "Test #1: Failed to update every minute. Reason:"
195 crontab -r >/dev/null 2>&1
196 break
197 else
198 echo " Expected $TS_MIN1; Received $TS_MIN2" \
199 > $LTPTMP/tst1_cron.log
200 $LTPBIN/tst_res TINFO $LTPTMP/tst1_cron.log \
201 "Test #1: Values are good: "
203 LOOP_CNTR=$(( $LOOP_CNTR-1 ))
204 done
206 if [ $FAILCNT -eq 0 ]
207 then
208 # check if var/log/messages file was updated.
209 grep "CMD ($LTPTMP/tst1_cronprg.sh)" /var/log/messages >$LTPTMP/cron_tst2n1.out 2>&1
210 RC=$?
211 #####
212 # Some implementations log cron info to /var/log/cron instead...
213 #####
214 if [ "$RC" -ne 0 -a -f /var/log/cron ]; then
215 $LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: alternate..."
216 grep "CMD ($LTPTMP/tst1_cronprg.sh)" /var/log/cron \
217 >$LTPTMP/cron_tst2n1.out 2>&1
218 RC=$?
220 if [ $RC -eq 0 ]
221 then
222 $LTPBIN/tst_resm TPASS \
223 "Test #1: installed cronjob, and cron executed the cronjob."
224 else
225 $LTPBIN/tst_res TFAIL $LTPTMP/cron_tst2n1.out \
226 "Test #1: Test failed. Reason:"
227 TFAILCNT=$(( $TFAILCNT+1 ))
229 else
230 $LTPBIN/tst_res TFAIL $LTPTMP/cron_tst1.out \
231 "Test #1: Cron did not execute every minute"
232 TFAILCNT=$(( $TFAILCNT+1 ))
235 #remove the cron job that was installed.
236 crontab -r >/dev/null 2>&1
239 # Test #2
240 # Test if crontab -r removes the installed crontab file
242 export TCID=cron02
243 export TST_COUNT=2
245 $LTPBIN/tst_resm TINFO "Test #2: crontab -r removes the crontab file."
247 cat > $LTPTMP/tst2_cronjob.cron <<EOF
248 * * * * * $LTPTMP/tst2_cronprg.sh
251 cat > $LTPTMP/tst2_cronprg.sh <<EOF
252 #! /bin/sh
254 echo "Hello Hell"
255 exit 0
258 chmod +x $LTPTMP/tst2_cronprg.sh >/dev/null 2>&1
260 $LTPBIN/tst_resm TINFO "Test #2: installing crontab file."
262 crontab $LTPTMP/tst2_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1
264 if [ $? -ne 0 ]
265 then
266 $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \
267 "Test #2: crontab Broke while installing cronjob. Reason:"
268 TFAILCNT=$(( $TFAILCNT+1 ))
271 sleep 10s
273 tail -n 10 /var/log/messages | grep crontab | grep REPLACE \
274 >$LTPTMP/cron_tst2n1.out 2>&1
275 RC=$?
276 #####
277 # Some implementations log cron info to /var/log/cron instead...
278 #####
279 if [ "$RC" -ne 0 -a -f /var/log/cron ]; then
280 $LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: alternate..."
281 tail -n 10 /var/log/cron | grep crontab | grep REPLACE \
282 >$LTPTMP/cron_tst2n1.out 2>&1
283 RC=$?
285 if [ $RC -ne 0 ]
286 then
287 $LTPBIN/tst_resm TFAIL \
288 "Test #2: crontab activity not recorded in var/log/messages."
289 TFAILCNT=$(( $TFAILCNT+1 ))
292 $LTPBIN/tst_resm TINFO "Test #2: uninstalling crontab file."
294 crontab -r >$LTPTMP/cron_tst2n1.out 2>&1
295 RC=$?
297 if [ $RC -ne 0 ]
298 then
299 $LTPBIN/tst_brk TBROK $LTPTMP/cron_tst2n1.out NULL \
300 "Test #2: crontab Broke while installing cronjob. Reason:"
301 TFAILCNT=$(( $TFAILCNT+1 ))
302 else
303 tail -n 10 /var/log/messages | grep DELETE >$LTPTMP/cron_tst2n1.out 2>&1
304 RC=$?
305 #####
306 # Some implementations log cron info to /var/log/cron instead...
307 #####
308 if [ "$RC" -ne 0 -a -f /var/log/cron ]; then
309 $LTPBIN/tst_resm TINFO "Test #1: /var/log/cron: alternate..."
310 tail -n 10 /var/log/cron | grep DELETE \
311 >$LTPTMP/cron_tst2n1.out 2>&1
312 RC=$?
314 if [ $RC -ne 0 ]
315 then
316 $LTPBIN/tst_resm TFAIL \
317 "Test #2: crontab activity not recorded in var/log/messages."
318 TFAILCNT=$(( $TFAILCNT+1 ))
319 else
320 $LTPBIN/tst_resm TPASS "Test #2: crontab removed the cronjob"
325 # Test #3
326 # Test if crontab -l lists the cronjob installed.
328 export TCID=cron03
329 export TST_COUNT=3
331 $LTPBIN/tst_resm TINFO "Test #3: crontab -l lists the cronjobs installed"
333 cat > $LTPTMP/tst2_cronjob.cron <<EOF
334 * * * * * $LTPTMP/tst2_cronprg.sh
337 cat > $LTPTMP/tst2_cronprg.sh <<EOF
338 #! /bin/sh
340 echo "Hello Hell"
341 exit 0
344 chmod +x $LTPTMP/tst2_cronprg.sh >/dev/null 2>&1
346 $LTPBIN/tst_resm TINFO "Test #3: installing crontab file ..."
347 crontab $LTPTMP/tst2_cronjob.cron >$LTPTMP/cron_tst2n1.out 2>&1
348 if [ $? -ne 0 ]
349 then
350 $LTPBIN/tst_brkm TBROK NULL \
351 "Test #3: crontab failed while installing cronjob"
352 TFAILCNT=$(( $TFAILCNT+1 ))
353 else
354 $LTPBIN/tst_resm TINFO "Test #3: Cron job installed."
357 crontab -l | grep "$LTPTMP/tst2_cronprg.sh" >$LTPTMP/cron_tst2n1.out 2>&1
358 RC=$?
359 if [ $RC -ne 0 ]
360 then
361 $LTPBIN/tst_brkm TBROK NULL \
362 "Test #3: crontab failed while listing cronjobs installed"
363 TFAILCNT=$(( $TFAILCNT+1 ))
364 else
365 $LTPBIN/tst_resm TINFO \
366 "Test #3: crontab -l listed cronjob tst2_cronprg.sh"
369 $LTPBIN/tst_resm TINFO "Test #3: uninstalling crontab file."
370 crontab -r >/dev/null 2>&1
372 if [ $? -ne 0 ]
373 then
374 $LTPBIN/tst_brkm TBROK NULL "Test #3: crontab failed while removing cronjob"
375 TFAILCNT=$(( $TFAILCNT+1 ))
378 crontab -l >$LTPTMP/cron_tst2.out 2>&1
379 if [ $? -ne 0 ]
380 then
381 grep "no crontab for" $LTPTMP/cron_tst2.out >$LTPTMP/cron_tst2n1.out 2>&1
382 RC=$?
383 if [ $RC -ne 0 ]
384 then
385 $LTPBIN/tst_res TFAIL $LTPTMP/cron_tst2n1.out \
386 "Test #3: crontab failed removing cronjob. Reason:"
387 TFAILCNT=$(( $TFAILCNT+1 ))
388 else
389 $LTPBIN/tst_resm TINFO "crontab uninstalled all jobs for user"
390 $LTPBIN/tst_resm TPASS "crontab did not list any cronjobs"
392 else
393 $LTPBIN/tst_res TFAIL $LTPTMP/cron_tst2n1.out \
394 "Test #3: crontab failed removing cronjob. Reason:"
395 TFAILCNT=$(( $TFAILCNT+1 ))
398 exit $TFAILCNT