Cron job is not running

Hi,

I have set up the crontab as follows.

[EMAIL="root@IDC4VASAPP07"]

root@IDC4VASAPP07 # crontab -l
0-59 * * * * /var/tmp/r.sh
0-59 * * * * date >> /var/tmp/log
root@IDC4VASAPP07 #

[/EMAIL]

r.sh is as follows.

[EMAIL="root@IDC4VASAPP07"]

 
root@IDC4VASAPP07 # cat r.sh
#!/bin/bash
dt1=$(perl -e 'use POSIX;print strftime "%d-%m-%Y-%M",localtime time-86400;')
mv local0.log local0.log.$dt1
mv local1.log local1.log.$dt1
mv local2.log local2.log.$dt1
mv local3.log local3.log.$dt1
mv local4.log local4.log.$dt1
mv local7.log local7.log.$dt1
mv snmpd.syslog snmpd.syslog.$dt1
echo "ran at $dt1" >> log
unset dt1
touch local0.log
touch local1.log
touch local2.log
touch local3.log
touch local4.log
touch local7.log
touch snmpd.syslog
root@IDC4VASAPP07 #

[/EMAIL]

But r.sh is not getting executed.

Please help.

Thanks,
Sunil

Post output of:

tail -50 /var/cron/log
1 Like

use the absolute path for the files

mv /absolute/path/local0.log local0.log.$dt1
mv /absolute/path/local1.log local1.log.$dt1
mv /absolute/path/local2.log local2.log.$dt1
1 Like

Following is the outpuit

[EMAIL="root@IDC4VASAPP07"]

 
root@IDC4VASAPP07 # tail -50 /var/cron/log
<  root 5088 c Sat Feb 11 03:17:00 2012
<  root 5087 c Sat Feb 11 03:17:00 2012
>  CMD: /var/tmp/r.sh
>  root 5130 c Sat Feb 11 03:18:00 2012
>  CMD: date >> /var/tmp/log
>  root 5131 c Sat Feb 11 03:18:00 2012
<  root 5131 c Sat Feb 11 03:18:00 2012
<  root 5130 c Sat Feb 11 03:18:00 2012
>  CMD: /var/tmp/r.sh
>  root 5173 c Sat Feb 11 03:19:00 2012
>  CMD: date >> /var/tmp/log
>  root 5174 c Sat Feb 11 03:19:00 2012
<  root 5174 c Sat Feb 11 03:19:00 2012
<  root 5173 c Sat Feb 11 03:19:00 2012
>  CMD: /var/tmp/r.sh
>  root 5216 c Sat Feb 11 03:20:00 2012
>  CMD: date >> /var/tmp/log
>  root 5217 c Sat Feb 11 03:20:00 2012
<  root 5217 c Sat Feb 11 03:20:00 2012
<  root 5216 c Sat Feb 11 03:20:00 2012
>  CMD: /var/tmp/r.sh
>  root 5262 c Sat Feb 11 03:21:00 2012
>  CMD: date >> /var/tmp/log
>  root 5263 c Sat Feb 11 03:21:00 2012
<  root 5263 c Sat Feb 11 03:21:00 2012
<  root 5262 c Sat Feb 11 03:21:01 2012
>  CMD: /var/tmp/r.sh
>  root 5309 c Sat Feb 11 03:22:00 2012
>  CMD: date >> /var/tmp/log
>  root 5310 c Sat Feb 11 03:22:00 2012
<  root 5310 c Sat Feb 11 03:22:00 2012
<  root 5309 c Sat Feb 11 03:22:00 2012
>  CMD: /bin/ksh /var/tmp/r.sh
>  root 5355 c Sat Feb 11 03:23:00 2012
<  root 5355 c Sat Feb 11 03:23:00 2012
>  CMD: /sbin/sh /var/tmp/r.sh
>  root 5400 c Sat Feb 11 03:24:00 2012
<  root 5400 c Sat Feb 11 03:24:00 2012 rc=2
>  CMD: /sbin/sh /var/tmp/r.sh
>  root 5430 c Sat Feb 11 03:25:00 2012
<  root 5430 c Sat Feb 11 03:25:00 2012 rc=2
>  CMD: /sbin/sh /var/tmp/r.sh
>  root 5464 c Sat Feb 11 03:26:00 2012
<  root 5464 c Sat Feb 11 03:26:00 2012 rc=2
>  CMD: /sbin/sh /var/tmp/r.sh
>  root 5497 c Sat Feb 11 03:27:00 2012
<  root 5497 c Sat Feb 11 03:27:00 2012 rc=2
>  CMD: /sbin/sh /var/tmp/r.sh
>  root 5531 c Sat Feb 11 03:28:00 2012
<  root 5531 c Sat Feb 11 03:28:00 2012 rc=2
You have new mail in /var/mail//root
root@IDC4VASAPP07 #

[/EMAIL]

---------- Post updated at 03:45 PM ---------- Previous update was at 03:36 PM ----------

Still not working after using the absolute paths.

[EMAIL="root@IDC4VASAPP07"]

 
root@IDC4VASAPP07 # cat r.sh
dt1=$(perl -e 'use POSIX;print strftime "%d-%m-%Y-%M",localtime time-86400;')
mv /var/tmp/local0.log /var/tmp/local0.log.$dt1
mv /var/tmp/local1.log /var/tmp/local1.log.$dt1
mv /var/tmp/local2.log /var/tmp/local2.log.$dt1
mv /var/tmp/local3.log /var/tmp/local3.log.$dt1
mv /var/tmp/local4.log /var/tmp/local4.log.$dt1
mv /var/tmp/local7.log /var/tmp/local7.log.$dt1
mv /var/tmp/snmpd.syslog /var/tmp/snmpd.syslog.$dt1
echo "ran at $dt1" >> /var/tmp/log
unset dt1
touch /var/tmp/local0.log
touch /var/tmp/local1.log
touch /var/tmp/local2.log
touch /var/tmp/local3.log
touch /var/tmp/local4.log
touch /var/tmp/local7.log
touch /var/tmp/snmpd.syslog
root@IDC4VASAPP07 #

[/EMAIL]

redirect your stderr and stdout

0-59 * * * * /var/tmp/r.sh 1>/tmp/out.txt 2>&1
1 Like

Probably a silly question, but is the script executeable by the user defined with the cron job?

As an aside, it is an odd definition you have because you explicitly say every minute with the range 0-59. Would an * not suffice?

Anyway, just a thought.

Robin
Liverpool/Blackburn
UK

The script is executing fine when running it as a root user from the command line.

---------- Post updated at 04:18 PM ---------- Previous update was at 04:16 PM ----------

Got the following error.

 
/var/tmp/r.sh: syntax error at line 1: `dt1=$' unexpected

But the above error is not thrown when running the script from the command line.

use back tick and check it once again

dt1=`perl -e 'use POSIX;print strftime "%d-%m-%Y-%M",localtime time-86400;'`
1 Like

After this change, the script is getting executed from Crontab.

Now I want to schedule the script to run at 00:01 hours every night.

So I set up the crontab as

1 0 * * * /sbin/sh /var/tmp/rotate.sh 1>/tmp/out.txt 2>&1

To test, I am changing the system date using the following command

date 021100002012

But the cron does not get executed & there is no error in the out.txt.

However if I change the crontab to

3 0 * * * /sbin/sh /var/tmp/rotate.sh 1>/tmp/out.txt 2>&1

& then change the system date, the cron job gets executed & again for the next iteration, I was required to change the cron entry to

5 0 * * * /sbin/sh /var/tmp/rotate.sh 1>/tmp/out.txt 2>&1

as the cron did not fire at 3 minutes past 0th hour & there were no errors in the out.txt file.

The cron deamon will still have the old clock. You will need to stop it. It may be defined in /etc/inittab as respawn in which case it will start a new one for you.

Robin
Liverpool/Blackburn
UK

1 Like

After re-starting the cron, it seems to be working fine. I will monitor the execution of cron job for few days.

Thanks to everyone for the help provided.