1834178 Members
2364 Online
110064 Solutions
New Discussion

Re: Run script error

 
heaman1
Regular Advisor

Run script error

I have a script run on unix and linux server , I found that if run it at shell , it works fine , but if run on crontab job , it is not work , can advise what is the different to run on shell and crontab job ? how can I fix it ? thx


the script like below

!/bin/ksh

if [ `date +%d` == 18 ]
then
xx

else
yy

fi
10 REPLIES 10
Aneesh Mohan
Honored Contributor

Re: Run script error

Hi,

Could be the path problem.Specify the absolute path for the commands when executing using cron.

/usr/bin/date

Thanks,
Aneesh

Paul McCleary
Honored Contributor

Re: Run script error

Hi,

Are you sure this script works?

I thought the test string comparison operator in sh/ksh was a single "=", not "==" like it is in say C.

You're also missing your # on your shebang.

When specifying the script you should use the absolute path in your crontab.

HTH, Paul
Ganesan R
Honored Contributor

Re: Run script error

Hi,

Since crontab won't inherit the path variables it will fail if absolute paths are not specified in the scripts.
Best wishes,

Ganesh.
Suraj K Sankari
Honored Contributor

Re: Run script error

Hi,

You have to specify full path of date
and moreover from crontab also you have to give the full path

Suraj
Dennis Handly
Acclaimed Contributor

Re: Run script error

>Suraj: You have to specify full path of date

No, /usr/bin is added to your crontab PATH.
Bill Hassell
Honored Contributor

Re: Run script error

cron (and at and batch) are commands to run processes on your behalf. But they do not login and will nothing from /etc/profile or your local .profile. Whenever you create a script that will be run without user intervention, always add the required environment, specifically, PATH and other variables. And then test your script with the batch command so you don't have to wait for cron. To use batch:

batch
/fullpath/to/your/script
CTRL-D

When you type batch, it gives you a blank line. Type in the name of your script and the return. Then type CTRL-D (the Ctrl key plus the letter d) and the script will run as a batch job. Once it works in batch, it should work OK in cron.


Bill Hassell, sysadmin
heaman1
Regular Advisor

Re: Run script error

thx replies ,

if run in cron that it do not run with /etc/profile & .profile , can advise how can I force the cron job run with it ?


thx
Dennis Handly
Acclaimed Contributor

Re: Run script error

>if run in cron that it does not run with /etc/profile & .profile, can advise how can I force the cron job run with it?

You can either change your script or change your crontab entry:
. /etc/profile
. ~/.profile

* * * * * . /etc/profile; ~/.profile; script-name parms ...

You might be able to get away without having /etc/profile.
heaman1
Regular Advisor

Re: Run script error

thx Dennis Handly , your method is simple and work for me .

Dennis Handly
Acclaimed Contributor

Re: Run script error

>your method is simple and work for me.

If you are happy with our answers, please read the following about assigning points:
http://forums.itrc.hp.com/service/forums/helptips.do?#33

Oops, I was missing a "." above:
* * * * * . /etc/profile; . ~/.profile; script-name parms ...