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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting the environment for CRON job

Status
Not open for further replies.

heprox

IS-IT--Management
Dec 16, 2002
178
US
With the help of several members of this forum, I have a file handling script that runs from crontab everynight. The script has one function that calls on another script called "prep". Whenever I'm logged in as myself I can run "prep" correctly, however crontab fails to find "prep" because its environment is different from mine. Is there a way inside my file handling script to use "setenv PATH=" to whatever my path is as long as that script is running? I would normally just give the script the full path to "prep" however "prep" is just a link that calls on several other scripts, all with different paths.

When I'm logged in my path looks like:
token$ printenv PATH
/gers/genret/menu/pub/sbin:/gers/genret/menu/pub/bin:/gers/genret/menu/pub/mac:/
gers/genret/menu/adm/sbin:/gers/genret/menu/adm/bin:/gers/genret/menu/adm/mac:/g
ers/genret/custom:/gers/genret/fix:/gers/genret/src_rev/fix:/gers/genret/opt/pat
h:/gers/genret/bin:/prod/mlink:/g/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin:.

 
Create a file with ALL needed variables setting and exporting. Then source (if sh/ksh: . command aka dot command) this file at the very beginning of the script called by cron.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I thought about using "su" orignally but I need the permissions that crontab has to chmod the files in order to process them correctly with the "prep" script.

PHV you are saying create a file like process.dat (or something) with the entry of:
setenv PATH=/gers/genret/menu/pub/sbin:/gers/genret/menu/pub/bin:/gers/genret/menu/pub/mac:/
gers/genret/menu/adm/sbin:/gers/genret/menu/adm/bin:/gers/genret/menu/adm/mac:/g
ers/genret/custom:/gers/genret/fix:/gers/genret/src_rev/fix:/gers/genret/opt/pat
h:/gers/genret/bin:/prod/mlink:/g/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin:.

...then in the script call EXPORT PATH?
 
In process.dat put all the needed variables, like this:
export PATH=/path/to/commands1:/path/to/commands2
export NeededVar="Usefull value"
In the beginning of the script add a line like this:
. /path/to/process.dat
BTW you can simply assign and export the needed variables directly in the script.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I thought about using "su" orignally but I need the permissions that crontab has to chmod the files in order to process them correctly with the "prep" script.

No you do not, cron run under root logs on as the user and executes the program. If it works, then it will run ok.

Just set up a user identical to your env, log on, test it,
and your good to go.
 
CDLVJ, I don't think su will work in this case because the environment is not being set by a .profile file but by an Oracle instance. When our users (including myself) log into this server their default program is a DBA menu. From there if I go to a shell I(in this case Korn) my environment is already set. If I'm in as the root user nad su to myself with "su - 'my username'", I get dumped into my initial program, the DBA menu.


PHV, what do you mean by
"export NeededVar="Usefull value"

...I created /usr/bin/process.dat with:

export PATH=/gers/genret/menu/pub/sbin:/gers/genret/menu/pub/bin:/gers/genret/menu/pub/mac:/
gers/genret/menu/adm/sbin:/gers/genret/menu/adm/bin:/gers/genret/menu/adm/mac:/g
ers/genret/custom:/gers/genret/fix:/gers/genret/src_rev/fix:/gers/genret/opt/pat
h:/gers/genret/bin:/prod/mlink:/g/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin:.

...my script has the following entry

/usr/bin/process.dat

...is that correct?

 
...my script has the following entry
/usr/bin/process.dat
...is that correct?

Nope.
First it's bad idea to hijack system directories like /usr/bin.
Second, I said you to source the file with the dot command and provided you an example in later post.
So, in your script either write this:
[red].[/red] /usr/bin/process.dat
or directly the export PATH stuff.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
put the following at the top of the script

#!/bin/ksh

PATH=$PATH:/new/path
export PATH

yourstuff


--
| Mike Nixon
| Unix Admin
|
----------------------------
 
heprox,

You could remark the menu command out in your .profile.

Normally users set up a prod usercode, so that they can run stuff like this.
 
After I placed the value for PATH in the script and exported it like:

#!/bin/ksh

PATH=$PATH:=/gers/genret/menu/pub/sbin:/gers/genret/menu/pub/bin:/gers/genret/menu/pub/mac:/
gers/genret/menu/adm/sbin:/gers/genret/menu/adm/bin:/gers/genret/menu/adm/mac:/g
ers/genret/custom:/gers/genret/fix:/gers/genret/src_rev/fix:/gers/genret/opt/pat
h:/gers/genret/bin:/prod/mlink:/g/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin
export PATH

...I'm still having path problems, I'm getting the following errors:

str_process[5]: gers/genret/menu/adm/sbin:/gers/genret/menu/adm/bin:/gers/genret
/menu/adm/mac:/g: not found.
+ ers/genret/custom:/gers/genret/fix:/gers/genret/src_rev/fix:/gers/genret/opt/p
at
str_process[6]: ers/genret/custom:/gers/genret/fix:/gers/genret/src_rev/fix:/ger
s/genret/opt/pat: not found.
+ h:/gers/genret/bin:/prod/mlink:/g/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin
str_process[7]: h:/gers/genret/bin:/prod/mlink:/g/bin:/usr/bin:/etc:/usr/sbin:/u
sr/ucb:/sbin: not found.

...somewhere I;m missing something. Perhaps use the "env" command (I'm on AIX 4.3.3)?
 
PATH=$PATH:=/gers/genret/menu/pub/sbin:/gers/genret/menu/pub/bin:/gers/genret/menu/pub/mac:/
gers/genret/menu/adm/sbin:/gers/genret/menu/adm/bin:/gers/genret/menu/adm/mac:/g
ers/genret/custom:/gers/genret/fix:/gers/genret/src_rev/fix:/gers/genret/opt/pat
h:/gers/genret/bin:/prod/mlink:/g/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin

Your PATH assignment is malformed:
1) It must be in single line
2) You must get rid of the = (equal sign) following $PATH:

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
It worked like a charm for the most part, however I continue to get the following error:

Can not open $ORACLE_HOME/trc/trans.out

I attempted to set the environment correctly with:

env -i ORACLE_HOME=/gers/genret

but I ended up with the same error again.

The script looks like:
#!/bin/ksh

env -i ORACLE_HOME=/gers/genret
PATH=$PATH:/gers/genret/menu/pub/sbin:/gers/genret/menu/pub/bin:/gers/genret/menu/pub/mac:/gers/genret/menu/adm/sbin:/gers/genret/menu/adm/bin:/gers/genret/menu/adm/mac:/gers/genret/custom:/gers/genret/fix:/gers/genret/src_rev/fix:/gers/genret/opt/path:/gers/genret/bin:/prod/mlink:/g/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin
export PATH


...then the rest?
 
env -i ORACLE_HOME=/gers/genret
This command does nothing. Try this:
export ORACLE_HOME=/gers/genret


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top