Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

environment in cron

Status
Not open for further replies.

surya12

Programmer
Jan 3, 2002
23
0
0
US
Hi

I have seen that environment(even after excuting .profile) is not available to crontab. Can you please help me?

I have put in task.sh:

/bin/ksh .profile
echo $PATH

and to my crontab:

10 * * * * task.sh

and the output gives:

/usr/bin for the echo $PATH.

Thanks in advance.
Surya
 
First thing is, you need to run the .profile "in the shell". And by that I mean preface it with a ".".

Second, you need to tell where your .profile file is.

Third, many commands that are commonly run in a .profile do not work in cron because you do not have a terminal. "stty" commands are an example.

Something like the lines below will work as long as there are no odd commands in .profile:

. /.profile
echo $PATH >> script.out
 

Thanks.
Well, I know that some commands like stty, export don't work, because the default shell provided is /bin/sh. That's why I used /bin/ksh to run the .profile. And then, it runs, but again the variabls are not available, because it runs in another shell.(I mean /bin/ksh .profile will be run in one shell and 'echo $PATH' in another shell). So, there's no use of running the .profile.

Is there another way to get the env variables. In fact my requirement is to run some database SQL's for some database maintenance.(I mean I have to run profile and some command like db2 -f myScript.sql and what it says is db2 not found)

Thanks.
Surya
 
The way I have done this in the past is to use cron to call a 'launching' script which includes the necessary PATH variable and then calls the script which you are trying to run. HTH.
 
Hi

As you said I have put in a file task.sh, and given to crantab as fololws:

#!/bin/ksh
PATH=SOMEPATH
DB2INSTANE=BLAH BLAH
DB2DIR=SOME_DIR
LD_LIBRARY_PATH=BLAH_BLAH

db2 -f someScript.sql

crontab produced output as:

SQL10007N Message "-1390" could not be retrieved. Reason code: "1".

I suppose it's not getting some variables.
Can someone please help.

Thanks,
Surya.
 
I might be missing the point here, but don't you just need to export your variables?? ( i.e preceed each declaration with 'export ' )
 
export is for ksh. The default shell provided is /bin/sh as I told previously.
 
However, in sh, you should use:

set=<variable> ; export <variable>

in much the same way. Cheers.
 
Sorry, that should be:

set <variable>=value ; export <variable>

It's getting late!
 


I changed the script to:

#!/bin/ksh
set PATH=SOMEPATH
set DB2INSTANE=BLAH BLAH
set DB2DIR=SOME_DIR
set LD_LIBRARY_PATH=BLAH_BLAH

export PATH
export DB2INSTANCE
export DB2DIR
export LD_LIBRARY_PATH

db2 -f someScript.sql

Now the cron output is: db2 not found

Regards,
Surya

 
...because you've overwritten your $PATH variable. You need to add to what was already in it. ( Also, your default shell being /bin/sh is academic - the first line in the script is invoking the korn shell. )
 
I have removed first line (/bin/ksh) and added the old PATH...even then it says: db2 not found

Thanks
Surya
 
Hi


task.sh:

set PATH=.:/export/home/snl00549/java1.3.1/bin:/export/home/snl00549/java1.3.1/jre/bin:/bin:/usr/openwin
/bin:/usr/bin:/usr/sbin:/usr/ccs/bin:/usr/ucb:/opt/SUNWspro/bin:/export/home/dbcc1/sqllib/bin:/export/ho
me/dbcc1/sqllib/adm:/opt/IBMdb2/V7.1/bin:/opt/IBMdb2/V7.1/adm:/opt/IBMdb2/V7.1/misc:/export/home/dbcc1/s
qllib/misc:$PATH:/export/home/snl00549:/opt/SUNWspro/man:$PATH
set DB2DIR=/export/home/dbcc1
export DB2DIR
set DB2PATH=/export/home/dbcc1/sqllib
export DB2PATH

set A4G_BASEDIRECTORY=/opt/GSTPAI1
export A4G_BASEDIRECTORY
set DB2INSTANCE=dbcc1
export DB2INSTANCE
set LD_LIBRARY_PATH=/export/home/dbcc1/sqllib/lib:/opt/GSTPAI1/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
set PATH=/opt/GSTPAI1/bin:/opt/IBMdb2/V7.1/bin:$PATH
export PATH

db2 -f /export/home/snl00549/task.sql

mycron:

38 * * * * /export/home/snl00549/task.sh


And I give the command as:

$ crontab mycron


Regards,
Surya
 
At this stage I can't see a clear reason why this shouldn't work, so I can only suggest doing some testing / debugging. If you are able to invoke db2 from your interactive shell, then use type to find it's path.

In your script, you can issue an echo $PATH just before the db2 call, and see if the db2 path is on it.

HTH
Mark
 

Hi

Alas! It's showing the PATH as /usr/bin: . That means there's is no use of setting and exporting variables.

I don't know what's happening. God only should help me.

What a useless crontab!!!!

-Surya

 
IMHO, there are 2 problems here. Firstly the use of 'set' - I do not think this is the correct usage. I would remove all 'set's, so that you declare each variable by it's name.

Secondly, I don't think adding your paths at the beginning of the string is correct. Normally you add them afterward. So, for example, one of your path settings would be:
PATH=$PATH:/opt/GSTPAI1/bin:/opt/IBMdb2/V7.1/bin

You could try that, and leave your debug message in to see how you get on.

Mark
 
It's wonder...magic. It worked fine.

In fact I tried in the beginning without using set. And then I didn't export them. And somebody suggested to use set.I had lost complete confidence, so I didn't much concentrate on it.

Many many thanks to Mark, for your 'do or die approach' and your ceaseless tries.

Also thnaks to all.

- Surya.
 
Glad it worked out. Sorry for the red herring re. set. Time I read the man page again I think!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top