1858007 Members
10526 Online
110382 Solutions
New Discussion

Re: Scripts

 
David McCallum
Occasional Advisor

Scripts

When I run a script from the command line the script runs fine. If I run the script from cron it does not work. Any suggestions??
Hello world!
7 REPLIES 7
Tim Medford
Valued Contributor

Re: Scripts

What is the script doing David?

I've had this problem sometimes when environment variables were different between the regular login shell and whatever shell runs for cron.

If this is the case, you may need to initialize/export the variables at the beginning of your script.

Tim
Tim Malnati
Honored Contributor

Re: Scripts

The most common issue with cron jobs is that .profile is NOT invoked and therefore the $PATH environment variable has not been extended with the additional directories that you use day in and day out from the command line. You can either extend the $PATH from within your cron script (don't forget to export it) or you can use direct references to all the commands used within the script.
Selvaraj
Advisor

Re: Scripts

This could happen due to insufficient execute permission also. But most of the time it happens due to .profile only. You can also execute the script and capture the screen output using :

For e.g:
# script /tmp/test.out
# ksh -x test.ksh

This helps us to find out where exactly problem occurs.
Have a nice day.
You can do it man !
CHRIS_ANORUO
Honored Contributor

Re: Scripts

Please check if the user has cron allow permissions and export PATH HOME and ENV settings in your .profile. The script should also have (#!/usr/bin/sh) depending on the shell that it is written/runs with.
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
Rick Garland
Honored Contributor

Re: Scripts

The PATH is a frequent cause of this issue. Goes back to the ENV not being set.

Can hard code the PATH into the commands or set a PATH env variable in the script
Alan Riggs
Honored Contributor

Re: Scripts

In addition to the PATH, there migt be other environmental variables required by the script (ORACLE_HOME, etc.) The simplest way to be certain that you have everything required for a script to run is to set the environment to mimic the user's standard login. Simply source the /etc/profile and users .profile (or .cshrc) files:

. /etc/profile
. /home//.profile
David McCallum
Occasional Advisor

Re: Scripts

The resolve to my problem was this. I am kicking the process off from the root cron and then switching the user environment to run the scritp.

cron entry:

IE: /usr/bin/su - username -c "script path"

Thanks to all those who gave answers in this matter.
Hello world!