Operating System - HP-UX
1819791 Members
3436 Online
109607 Solutions
New Discussion юеВ

Diff between running something tru CRON and prompt

 
SOLVED
Go to solution
Kamran Hussain
Occasional Advisor

Diff between running something tru CRON and prompt

I am getting this error, and the script aborts:

test: argument expected

when I run the script from cron, and it runs fine if run from a command prompt.

What should I look for ?
14 REPLIES 14
Bill McNAMARA_1
Honored Contributor

Re: Diff between running something tru CRON and prompt

could be shell environment variables..
run env at the top of the script.

Later,
Bill
It works for me (tm)
Chris Wilshaw
Honored Contributor

Re: Diff between running something tru CRON and prompt

Cron only runs with the minimal environment already set.

Normal logins will have the details from /etc/PATH, /etc/profile and a number of other config files set, which cron will not.

In order to run things in cron as you would at the command line, you need to make sure that any relevant variables are set at the start of the cron job

eg:

PATH=`cat /etc/PATH`

A. Clay Stephenson
Acclaimed Contributor

Re: Diff between running something tru CRON and prompt


First of all, the test or if [ operand should be enclosed in "" so that the behavior is better defined for null arguments. You fundamental problem is almost certainly an undefined environment variable. Note: cron or at does not source a user's .profile (and generally it should not do this explicitly because .profile's often have commands which expect a terminal connection. The correct way to deal with this is to have a file like /usr/local/bin/myenv.sh which sets and exports any needed varaiables and then this file should be sourced (using . /usr/local/bin/myenv.sh) by both the user's .profile and your cron'ed script.
If it ain't broke, I can fix that.
John Bolene
Honored Contributor

Re: Diff between running something tru CRON and prompt

Nothing is default as there are no profiles sourced.

I normally run cron jobs with full pathnames and whatever variables set that it is expecting.
It is always a good day when you are launching rockets! http://tripolioklahoma.org, Mostly Missiles http://mostlymissiles.com
Kamran Hussain
Occasional Advisor

Re: Diff between running something tru CRON and prompt

Thank you for your input. I tried to set the env variable that are setup in my .profile, within the cron'd scritps...but I am still getting that same error. Here is the code, may be someone will pick something out:

(The error line has *** below, and this is my first line in the script:
#!/usr/bin/ksh)

if ( [[ $todays_day = "Sat" ]] && (( $todays_time > 6 )) ) || ( [[ $todays_day = "Sun" ]] ) || ( [[ $todays_day = "Mon" ]] && (( $todays_time < 8 )) )
then
if ( [[ $todays_day = "Sat" ]] && (( $todays_time > 7 )) ) && ( [[ $todays_day = "Sat" ]] && (( $todays_time < 12 )) )
then
***if [ $ora_err_num = ORA-12012: ]
then
weekend=n
fi;
fi;
else
weekend=y
fi;
Pete Randall
Outstanding Contributor

Re: Diff between running something tru CRON and prompt

Where is $ora_err_num being set?

Pete

Pete
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Diff between running something tru CRON and prompt

Remember what I said about the quotes?

if [ $ora_err_num = ORA-12012: ]
should be

if [ "${ora_err_num}" = "ORA-12012:" ]

I assume that all that date stuff is being set by the 'date' command; make sure that it is in your PATH.

The fundamental problem here is that $ora_err_num is almost certainly not being set.

If it ain't broke, I can fix that.
Kamran Hussain
Occasional Advisor

Re: Diff between running something tru CRON and prompt

All variables are being set earlier in this script. Here is the one you asked about:
ora_err_num=`echo $x1`

Please keep in mind that this scritps runs great from a prompt !
James R. Ferguson
Acclaimed Contributor

Re: Diff between running something tru CRON and prompt

Hi:

It appears that 'ora_err_num' is null. If you use quote marks ("") to surround the variable, then your then the test occur even if the variable is null. Quoting variables in test statements is strongly urged:

if [ "$ora_err_num" = ORA-12012: ]

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: Diff between running something tru CRON and prompt

Try putting "echo $ora_err_num" in the script and then run it both ways - I think you'll see it's not set when run by cron. You'll have to source whatever environment script sets that variable into your script.

Pete

Pete
Wodisch
Honored Contributor

Re: Diff between running something tru CRON and prompt

Hi,

without further checking your script:

ALWAYS quote all substitutions in scripts!

"test" is just to prone to bark on those *empty* substitutions...

FWIW,
Wodisch
Kamran Hussain
Occasional Advisor

Re: Diff between running something tru CRON and prompt

You all are great - I have never seen such a live and quick responding forum ! Thank you so much.

The problem was the variable not being set, because there were no error in the log that I was scanning. And the reason it was working from the prompt (vs. CRON) was becuase I was referencing a file in the local dir (which did not have the full path defined).

In conclusion, which is the better way /more accurate way to avoid the variable having a null value:

if [ "${ora_err_num}" = "ORA-12012:" ]

--OR--

if [ "$ora_err_num" = ORA-12012: ]
Pete Randall
Outstanding Contributor

Re: Diff between running something tru CRON and prompt

if [ "${ora_err_num}" = "ORA-12012:" ]

Pete
A. Clay Stephenson
Acclaimed Contributor

Re: Diff between running something tru CRON and prompt

The ${var} convention is one that you should adopt in every case. You will see the problem with something like this: $var1$var2 that should be and will be correctly interpreted if expressed like this: ${var1}${var2}.
You should simply get in the habit of enclosing every shell variable in {}'s.

Enclosing possible null test operands in quotes ensures that you will not generate a shell syntax error and terminate the shell but there may still be a problem with the data itself. You could detect that error with
if [[ -n "${var} ]] and deal with it inside your script.
If it ain't broke, I can fix that.