3 ################################################################################
5 ## Copyright (c) International Business Machines Corp., 2001 ##
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. ##
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. ##
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 ##
22 ################################################################################
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
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'
46 if [ -z "$LTPTMP" -a -z "$TMPBASE" ]
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.
63 # Test if crontab <filename> installs the crontab file and cron schedules the
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
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
86 echo "Hello Hell today is \$DATE " > $LTPTMP/tst1_cron.out 2>&1
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
96 $LTPBIN/tst_resm TINFO
"Test #1: Installing cron job ... "
97 crontab
$LTPTMP/tst1_cronjob.cron
>$LTPTMP/cron_tst2n1.out
2>&1
102 $LTPBIN/tst_brk TBROK
$LTPTMP/cron_tst2n1.out NULL \
103 "Test #1: crontab Broke while installing cronjob. Reason:"
104 TFAILCNT
=$
(( $TFAILCNT+1 ))
106 $LTPBIN/tst_resm TINFO
"Test #1: Cronjob installed successfully"
111 tail -n 10 /var
/log
/messages |
grep crontab |
grep REPLACE \
112 > $LTPTMP/cron_tst2n1.out
2>&1
115 # Some implementations log cron info to /var/log/cron instead...
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
125 $LTPBIN/tst_resm TFAIL \
126 "Test #1: crontab activity not recorded in /var/log/messages."
127 TFAILCNT
=$
(( $TFAILCNT+1 ))
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.
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...
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.
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.
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" ]
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 ))
179 if [ $TS_MIN1 -eq 59 ]
183 TS_MIN1
=$
(( $TS_MIN1+1 ))
186 if [ $TS_MIN2 -ne $TS_MIN1 ]
188 # if the value of the minute field did not advance by 1
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
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 ))
206 if [ $FAILCNT -eq 0 ]
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
212 # Some implementations log cron info to /var/log/cron instead...
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
222 $LTPBIN/tst_resm TPASS \
223 "Test #1: installed cronjob, and cron executed the cronjob."
225 $LTPBIN/tst_res TFAIL
$LTPTMP/cron_tst2n1.out \
226 "Test #1: Test failed. Reason:"
227 TFAILCNT
=$
(( $TFAILCNT+1 ))
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
240 # Test if crontab -r removes the installed crontab file
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
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
266 $LTPBIN/tst_brk TBROK
$LTPTMP/cron_tst2n1.out NULL \
267 "Test #2: crontab Broke while installing cronjob. Reason:"
268 TFAILCNT
=$
(( $TFAILCNT+1 ))
273 tail -n 10 /var
/log
/messages |
grep crontab |
grep REPLACE \
274 >$LTPTMP/cron_tst2n1.out
2>&1
277 # Some implementations log cron info to /var/log/cron instead...
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
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
299 $LTPBIN/tst_brk TBROK
$LTPTMP/cron_tst2n1.out NULL \
300 "Test #2: crontab Broke while installing cronjob. Reason:"
301 TFAILCNT
=$
(( $TFAILCNT+1 ))
303 tail -n 10 /var
/log
/messages |
grep DELETE
>$LTPTMP/cron_tst2n1.out
2>&1
306 # Some implementations log cron info to /var/log/cron instead...
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
316 $LTPBIN/tst_resm TFAIL \
317 "Test #2: crontab activity not recorded in var/log/messages."
318 TFAILCNT
=$
(( $TFAILCNT+1 ))
320 $LTPBIN/tst_resm TPASS
"Test #2: crontab removed the cronjob"
326 # Test if crontab -l lists the cronjob installed.
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
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
350 $LTPBIN/tst_brkm TBROK NULL \
351 "Test #3: crontab failed while installing cronjob"
352 TFAILCNT
=$
(( $TFAILCNT+1 ))
354 $LTPBIN/tst_resm TINFO
"Test #3: Cron job installed."
357 crontab
-l |
grep "$LTPTMP/tst2_cronprg.sh" >$LTPTMP/cron_tst2n1.out
2>&1
361 $LTPBIN/tst_brkm TBROK NULL \
362 "Test #3: crontab failed while listing cronjobs installed"
363 TFAILCNT
=$
(( $TFAILCNT+1 ))
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
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
381 grep "no crontab for" $LTPTMP/cron_tst2.out
>$LTPTMP/cron_tst2n1.out
2>&1
385 $LTPBIN/tst_res TFAIL
$LTPTMP/cron_tst2n1.out \
386 "Test #3: crontab failed removing cronjob. Reason:"
387 TFAILCNT
=$
(( $TFAILCNT+1 ))
389 $LTPBIN/tst_resm TINFO
"crontab uninstalled all jobs for user"
390 $LTPBIN/tst_resm TPASS
"crontab did not list any cronjobs"
393 $LTPBIN/tst_res TFAIL
$LTPTMP/cron_tst2n1.out \
394 "Test #3: crontab failed removing cronjob. Reason:"
395 TFAILCNT
=$
(( $TFAILCNT+1 ))