Operating System - HP-UX
1835036 Members
4013 Online
110073 Solutions
New Discussion

Running a command via cron vs manual

 
SOLVED
Go to solution
Mark Cook
Advisor

Running a command via cron vs manual

I was wondering why a script would run sucessfully if ran from the command line, however, the same script scheduled in cron dose not produce the same results if any at all. I have a script named "ck_syslog.sh" which checks syslog.log for certain keywords. If any of the defined key words are found, then it sends me an e-mail with the results. This works everytime running from the command line, however, if I schedule it using cron I have never received the proper resluts. Any suggestions? Thanks, in advacne, for everyone's/anyone's help!!! Mark
"The prerequisite for action is the will and courage to be TRUTHFUL!"
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: Running a command via cron vs manual

It's really very simple. The environment under cron is very sparse. Your script neets to
set and export PATH and any other variables you might need. (e.g. SHLIB_PATH, ORACLE_HOME,
ORACLE_SID, etc.) Typically all you need is PATH.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Running a command via cron vs manual

Hi Mark:

The environment which is provided to cron does not include the environmental variables you have when you have logged-in (for instance) and therefore sourced your $HOME profile.

Environmental variables can be embedded in your script as necessary, or they can be specified and exported or file-sourced in a crontab entry in one of the following ways:

0 1 * * * (V=x;export V;/home/my/script)

0 1 * * * (. /home/my/.env /home/my/script)

0 1 * * * su -c -c /home/my/script

...JRF...
Mark Cook
Advisor

Re: Running a command via cron vs manual

First off, thanks A.Clay Stephenson & James Ferguson for both of your quick replies. When I look at other scripts they may have the following:
PATH=/usr/bin:/usr/sbin:/sbin:/usr/omni/bin:/opt/omni/bin:/usr/local/bin
Is this what you are refering to and if so, can you please indicate why this is necessary. I have certain scripts currently in cron that work to succession without this type of entry. Thanks again gentlemen!
"The prerequisite for action is the will and courage to be TRUTHFUL!"
A. Clay Stephenson
Acclaimed Contributor

Re: Running a command via cron vs manual

You are on the right track. You need to add a
couple of lines much like this to your script:
PATH=/usr/bin:/usr/local/bin:/opt/omni/bin
export PATH
You may need other directories as well depending on where the executables are located.

You can get a clear idea of what is going on by creating a small script, 'my.sh'.
#!/usr/bin/sh
echo "Path = ${PATH}" > /tmp/path.txt

chmod 755 my.sh
Execute my.sh from the shell and examine /tmp/path.txt.
Next create a crontab entry to execute my.sh
and examine /tmp/path.txt again. I think that you will find that the PATH's are vastly different.

Clay
If it ain't broke, I can fix that.
Mark Cook
Advisor

Re: Running a command via cron vs manual

Thanks again...I will do that and if you don't ever hear from me again, then you know it worked. Thanks for your time and efforts!
"The prerequisite for action is the will and courage to be TRUTHFUL!"