Operating System - HP-UX
1843944 Members
2437 Online
110226 Solutions
New Discussion

Script not running in cron

 
Deepak.R
Frequent Advisor

Script not running in cron

Hi,

I have a shell script in cron which is not running well. It runs fine when executed from shell.

script -

#! /bin/sh
#
QUEUENAME=dhcpmgt
XACTDIR=/var/opt/nid1/output

ls -1rt $XACTDIR | while read file
do
if `/usr/bin/lp -c -d$QUEUENAME $XACTDIR/$file > /dev/null`
then
rm $XACTDIR/$file
fi
done
-------------------------

cron entry

0,10,16,20,25,30,35,40,50 * * * * /opt/nid1/dhcp-fwd.sh > /dev/null

---------------

/var/adm/cron/log : -

> CMD: /opt/nid1/dhcp-fwd.sh > /dev/null 2>/tmp/err
> root 9866 c Thu Jan 5 15:40:01 EST 2006

> CMD: /opt/nid1/dhcp-fwd.sh > /dev/null 2>/tmp/err
> root 15215 c Thu Jan 5 16:00:00 EST 2006
< root 15215 c Thu Jan 5 16:00:00 EST 2006 ts=9
< sspe 15214 c Thu Jan 5 16:00:12 EST 2006 ts=9


pls help.



17 REPLIES 17
TwoProc
Honored Contributor

Re: Script not running in cron

Can you take the output that's going to /dev/null and send it to a collection file and post it for us?
We are the people our parents warned us about --Jimmy Buffett
baiju_3
Esteemed Contributor

Re: Script not running in cron

Modify the cron entry

0,10,16,20,25,30,35,40,50 * * * * /opt/nid1/dhcp-fwd.sh > /dev/null


and remove the redcirection

0,10,16,20,25,30,35,40,50 * * * * /opt/nid1/dhcp-fwd.sh.

See it makes any difference .

thx,
bl.
Good things Just Got better (Plz,not stolen from advertisement -:) )
Geoff Wild
Honored Contributor

Re: Script not running in cron

Change cron entry from

0,10,16,20,25,30,35,40,50 * * * * /opt/nid1/dhcp-fwd.sh > /dev/null

to

0,10,16,20,25,30,35,40,50 * * * * /opt/nid1/dhcp-fwd.sh > /tmp/dhcp-fwd.cronlog

then post /tmp/dhcp-fwd.cronlog

Most cases - this is an ENV or PATH issue.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Deepak.R
Frequent Advisor

Re: Script not running in cron

Tried redirecting output and errors to a file but nothing gets logged in that.
Geoff Wild
Honored Contributor

Re: Script not running in cron

Sorry - should be:

,10,16,20,25,30,35,40,50 * * * * /opt/nid1/dhcp-fwd.sh > /tmp/dhcp-fwd.cronlog 2>&1

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Tom Danzig
Honored Contributor

Re: Script not running in cron

You did not supply the correct lines from the cron log.

/var/adm/cron/log : -

> CMD: /opt/nid1/dhcp-fwd.sh > /dev/null 2>/tmp/err
> root 9866 c Thu Jan 5 15:40:01 EST 2006

> CMD: /opt/nid1/dhcp-fwd.sh > /dev/null 2>/tmp/err
> root 15215 c Thu Jan 5 16:00:00 EST 2006
< root 15215 c Thu Jan 5 16:00:00 EST 2006 ts=9
< sspe 15214 c Thu Jan 5 16:00:12 EST 2006 ts=9

The command ran via cron with PID: 9866. You gave the termination info for PID: 15214 (which appears to have been killed via a kill -9)

Find the termination line in the cron log from the 9866 PID.

I would also redirect stdout and stderr to a file for inspection also.
Sanjay_6
Honored Contributor

Re: Script not running in cron

Hi,

Are there any directories in "var/opt/nid1/output". The cron job is getting a sigkill "ts=9".

You can try exporting the user .profile in the script and see if that helps.

-------------------------------
#/usr/bin/ksh

. /path_to_home_dir/.profile

.....
your script
-------------------------------

Hope this helps.

regds
Deepak.R
Frequent Advisor

Re: Script not running in cron

Didnt work
john korterman
Honored Contributor

Re: Script not running in cron

Hi,

check mail messages for the cron user; it should give a clue.

regards,
John K.
it would be nice if you always got a second chance
john korterman
Honored Contributor

Re: Script not running in cron

you could also try to redirect error messages in your if-sentence to dev/null, e.g.:

if `/usr/bin/lp -c -d$QUEUENAME $XACTDIR/$file 2> /dev/null`

regards,
John K.
it would be nice if you always got a second chance
Raynald Boucher
Super Advisor

Re: Script not running in cron

Do the files get deleted but not printed?

The "if" statement could be returning a different code than expected because two results are inspected: the print and the redirect. The redirect is always successfull.

Maybe someone else can explain why this would work differently when issued in a "batch" or "terminal" environment...

Try removing the redirect as suggested earlier. The cron user will have mail to report activity.
Vipulinux
Respected Contributor

Re: Script not running in cron

Try changing the redirection file from /dev/null to /tmp/test.log

check the mail for cron and the file for any issues...


Cheerz
Arturo Galbiati
Esteemed Contributor

Re: Script not running in cron

Hi,
load the .profile in script and try again.
remeber that crontab doesn't load the .profile of teh user.
HTH,
Art
Muthukumar_5
Honored Contributor

Re: Script not running in cron

Try like this:

#! /bin/sh
#
QUEUENAME=dhcpmgt
XACTDIR=/var/opt/nid1/output

ls -1rt $XACTDIR | while read file
do
/usr/bin/lp -c -d$QUEUENAME $XACTDIR/$file 1> /dev/null 2>&1
if [[ ${?} -eq 0 ]]
then
# Uncomment
# echo "Action Completed" | mail mailID
rm $XACTDIR/$file
fi
done

exit 0

##########

0,10,16,20,25,30,35,40,50 * * * * /opt/nid1/dhcp-fwd.sh 1> /dev/null 2>&1
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Script not running in cron

Try like this:

#! /bin/sh
#
QUEUENAME=dhcpmgt
XACTDIR=/var/opt/nid1/output

ls -1rt $XACTDIR | while read file
do
/usr/bin/lp -c -d$QUEUENAME $XACTDIR/$file 1> /dev/null 2>&1
if [[ ${?} -eq 0 ]]
then
# Uncomment
# echo "Action Completed" | mail mailID
rm $XACTDIR/$file
fi
done

exit 0

##########

0,10,16,20,25,30,35,40,50 * * * * /opt/nid1/dhcp-fwd.sh 1> /dev/null 2>&1

--
Muthu
Easy to suggest when don't know about the problem!
Oviwan
Honored Contributor

Re: Script not running in cron

Hi

change the cron to:

0,10,16,20,25,30,35,40,50 * * * * su - USERNAME -c "/opt/nid1/dhcp-fwd.sh > /dev/null"

Regards
Darren Prior
Honored Contributor

Re: Script not running in cron

I would suggest putting a "set -x" below the shell definition line (obviously as well as changing the redirection so that you have stdout and stderr going to a file.) You can then see the effect of each line.

regards,

Darren.
Calm down. It's only ones and zeros...