Operating System - HP-UX
1847247 Members
2971 Online
110263 Solutions
New Discussion

Running a Script from cron

 
David McCallum
Occasional Advisor

Running a Script from cron

When I run a script from the command line manually it works fine. If I run the same script from cron it does not work. Any suggestions??
Hello world!
16 REPLIES 16
Andreas Voss
Honored Contributor

Re: Running a Script from cron

Hi,

the major common difference between command line (shell from login) and cron is the environment (can be seen with: set)
So check your script if it depends on environment variables that where not set by cron. Add these variable definitions to your script.
Another thing is that a process from cron has no input device (not tty assosiation). So getting some dialog input is not available by cron.

Hope this gives you the way.

Regards
Anthony Goonetilleke
Esteemed Contributor

Re: Running a Script from cron

You might want to source any info from your .profile in your cron script
Minimum effort maximum output!
Mr Anthony Wong
New Member

Re: Running a Script from cron

In the most cases in my experience, the reason of script failed using cron is due to invalid path of command.
Those command you can run smoothly on a shell becase $PATH has been setup on .profile. But it may not use when running scripts via cron. So, I would suggest all command inside the script should key in path. e.g. /etc/fbackup or /etc/vgdisplay.
Anthony

Re: Running a Script from cron

Hi,

I think the first thing to do is look at you mail. Most of the times, your mail will contain error messages that will show why your script did not run as expected. Most probably, the script cannot find your commands or other env. variables.
Paul Davies
Advisor

Re: Running a Script from cron

Hi,

Make sure you are running the correct shell. Cron runs jobs by default with /usr/bin/sh so if you have written and tested a script using your csh or ksh login shell it will not work.

The first line should specify the shell.ie

#!/usr/bin/ksh

Also, as the other answers have said I find that paths and environment settings are a common problem.

Try redirecting stderr to a file to see what the problem is. eg

/script 2>/error.report

Hope it helps,


Paul.
David McCallum
Occasional Advisor

Re: Running a Script from cron

Thanks for all the input. After tracing through the script I know the problem is loging into sql. This command works from the prompt.
sqlplus username/password @/opt/mimsps/mims_sys/local/script_name

In cron I get this error which tells me it must be the environment variables.
/opt/mimsps/mims_sys/local/usbtest.sh[55]: sqlplus: not found

Any other suggestions??

Hello world!
Lawrence Mahan
Frequent Advisor

Re: Running a Script from cron

In your .profile of the user that can run the script (or in /etc/profile) you of course setup your enviroment. Just copy the enviroment setup lines to the top of your script and it should then be able to run in cron. Try to be surgical as to what enviroment you build in the script. Only put in what the script needs.

You may also want to set your shell in the script with the #!/sbin/ksh line or what ever shell you actually need.
Madhu Sudhan_1
Respected Contributor

Re: Running a Script from cron

David:
Its clearly the problem with the PATH variable not being set.
Add the following lines to the script begining.
export PATH=$PATH:$ORACLE_HOME/bin.
Also ensure to define ORACLE_HOME variable just before the above line in your script according to your environment.

This should make the script work when placed in cron.

......Madhu
Think Positive
Alan Riggs
Honored Contributor

Re: Running a Script from cron

The problem is that the shell does not find sqplplus in its PATH. Either set the PATH ecplicitely or supply an absolute path to sqlplus on the line in the script.
David McCallum
Occasional Advisor

Re: Running a Script from cron

Thanks again for the information. I added these lines to the script.

ORACLE_HOME=/oracle/7.3.3
export PATH=$PATH:$ORACLE_HOME/bin

Now I get this error message.

Message file sp1.msb not found.
Error initializing SQL*Plus

Any last suggestions??
Hello world!
Alan Riggs
Honored Contributor

Re: Running a Script from cron

It appears the sqplus command depends upon something else that is set in your environment. Rather than knocking each of these issues down one-by-one might I suggest placing:

. /etc/profile
. /home//.profile

at the beginning of your script.
David McCallum
Occasional Advisor

Re: Running a Script from cron

Thanks again for the information. I added these lines to the script.

ORACLE_HOME=/oracle/7.3.3
export PATH=$PATH:$ORACLE_HOME/bin

Now I get this error message.

Message file sp1.msb not found.
Error initializing SQL*Plus

Any last suggestions??
Hello world!
Madhu Sudhan_1
Respected Contributor

Re: Running a Script from cron

Add the following path to resolve the problem problem of sqlplus not getting initialised.

As a whole it should look something like the following
PATH=$PATH:$ORACLE_HOME:/bin:$ORACLE_HOME/sqlplus/mesg

Hope fully this works.

......Madhu
Think Positive
David McCallum
Occasional Advisor

Re: Running a Script from cron

The resolve to my issue was to run the script from the root cron and then su to the user environment to run the script

crontab entry:

00 5 5 * * /usr/bin/su - username -c "script"

Thanks to all those who gave answers in this matter.
Hello world!
Kelly Wiebe
Occasional Advisor

Re: Running a Script from cron

I had this same problem with executing an sql script from an automatic action in OVO. To solve it I added the following lines to my script.

export ORACLE_HOME=
export ORACLE_SID=openview
export PATH=$ORACLE_HOME/bin:$PATH
export ORAENV_ASK=NO
. oraenv

And now it works.
start a day with a disaster then end it with another disaster
Paulo A G Fessel
Trusted Contributor

Re: Running a Script from cron

If you're using Oracle, you must declare exactly the environment variables that are present on your default Oracle user environment.

The easiest way to do this is to include the line

. ~orauser/.profile (or . ) in your script. A colleague had the same problem here yesterday and this has solved the issue.

HTH
Paulo Fessel
L'employé propose, le boss dispose.