Operating System - HP-UX
1821544 Members
2179 Online
109633 Solutions
New Discussion юеВ

Re: crontab and user environment

 
SOLVED
Go to solution
Eduardo Uribe_1
Occasional Advisor

crontab and user environment

Hi Guys!

I was doing some shells and I am able to execute them fine from the command line and it works great.
Once I added the line that executes de shell in the users's cron it doesn't work correctly.
I'm not sure if it is the environment wich doesn't load correctly from the cron...
It would be great if anyone could give me a push, also youcan ask for further info about the shell I know it's hard to understand the problem without seeing the code.
I would tell you that it doesn't even execute sqlplus wich is embedded in the shell.
Here's the mail that cron daemons sends when it tries to execute the shell...
Thanx in advance.



Bases_de_datos[13]: sqlplus: not found
/users/infra/SOPORTE/ED/monitor/log_creator.sh[18]: ./outbox/alertas.eml: cannot create
/users/infra/SOPORTE/ED/monitor/log_creator.sh[18]: ./outbox/alertas.eml: cannot create


*************************************************
Cron: The previous message is the standard output
and standard error of one of your crontab commands:

/users/infra/SOPORTE/ED/monitor/gmon /users/infra/SOPORTE/ED/monitor



Thanx again.

Ed
7 REPLIES 7
Victor_5
Trusted Contributor

Re: crontab and user environment

Hi,

There is another post on similiar issue, maybe it is useful for you.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0x67a972234586d5118ff00090279cd0f9,00.html

Rodney Hills
Honored Contributor

Re: crontab and user environment

A shell script launched from cron will not have PATH variable fully defined like a regular terminal login.

Either set PATH to where your executables are or specify the full path on commands and files. ie /opt/oracle/bin/sqlplus

-- Rod H
There be dragons...
Rainer von Bongartz
Honored Contributor

Re: crontab and user environment

cron does not take you environement !!!!
you have to set everything that yout shell has in your cron scrip, otherwise your script won't run.

You could do something like the following:

1. collect your environement in a file
env > /tmp/en

2. insert this file at the beginning of you cron script

3. try it




He's a real UNIX Man, sitting in his UNIX LAN making all his UNIX plans for nobody ...
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: crontab and user environment

Hi Eduardo,

This is a very common occurence. The environment under which a cron job is run is intentionally very sparse.

You script need's to set ORACLE_HOME, ORACLE_SID, ORACLE_BASE, TNS_ADMIN, PATH, ...

My favorite way to do this is to create a script in /usr/local/bin (e.g.) oraenv.sh
something like this:

export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=${ORACLE_BASE}/product/8.1.7

(Make sure you do not put an exit statement in the file)

and anything else

your cron script should then source this file
. /usr/local/bin/oraenv.sh
you should then set PATH
and start up script code

any database user should also source this file as part of their .profiles


Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: crontab and user environment

Hi:

'cron's environment lacks all of your environmental variables (including your PATH) that you normally rely on (and forget about) when you are working in your normal login shell.

There are several ways to circument this and obtain a similar environment.

You can source your profile ahead of calling your script. This can be done in the crontab entry. Alternatively, you can source your profile within the script itself to provide the necessary environmental variables you need. Thirdly, you can create a new file which contains and exports the common variables needed for your application, and source (read) that within your standard profile, within standard scripts; and/or in a crontab stream.

To source (read) a file within a script, put a dot (".") in front of the script name. For instance, to source a file called /myscript you would do:

. /myscript #...note the space between the dot and the script name.

...JRF...
Praveen Bezawada
Respected Contributor

Re: crontab and user environment

Hi
When executing using cron all the environment setting have to be set explicitly in the script. As cron does not login to run the script the environment with not be set.
Simply put, all the settings in the your .profile need to be in the script.
Magdi KAMAL
Respected Contributor

Re: crontab and user environment

Hi,

You must execute, as first instruction in the crontab script, .profile file in order to have the environment variables set.

without doing that, only four environment variables are set.

Magdi