Operating System - HP-UX
1819927 Members
3255 Online
109607 Solutions
New Discussion юеВ

jobs via cron and their path

 
SOLVED
Go to solution
Peter Gillis
Super Advisor

jobs via cron and their path

Hi....
system D390
Op sys - ux11.00
I have a job when I run using the at cmd is successful - appears to travel the correct paths and picks up the correct values for env variables. But, (there always appears to be one of these!) when I leave the job to run under instruction from cron it fails because it is not picking up the correct environement variable. Does anyone have any suggestions as to how I can work out where the job is picking up the wrong env variable. ?

Appreciate your help with this one.
Thanks, Maria.
3 REPLIES 3
Patrick Wallek
Honored Contributor

Re: jobs via cron and their path

Easiest way is to make SURE that you declare ANY and ALL environment variables that your script needs within the script.

It's probably picking something up from your .profile that doesn't necessarily get seen via cron.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: jobs via cron and their path

Cron has an intentionally sparse environment. Don't be tempted to do an su - username command inside a script called by cron because it will almost certainly fail. You have two options: 1) set an export all the enviroment variables including PATH inside your cron script or 2) - better - allow your cron script to source a file e.g. . /usr/local/bin/myenv.sh that sets and exports any needed variables. There should be no return or exit statements in this sourced file. You could also choose to source this same file from any desired user's .profile so that you only have to make any changes to the environment in one place.

You are experiencing perfectly normal behavior for cron. The other common error is invoking commands which expect to be connected to sort sort of serial device like a terminal and you don't have one under cron.

If it ain't broke, I can fix that.
Patrick Chim
Trusted Contributor

Re: jobs via cron and their path

Hi,

Sometimes in your convenient, you always set your environment variable in .profile or some other file that will executed by .profile. So when you logon to the system, you will always get all the setting you want.

But when you executed the job in cron, it just 'run' the job ONLY and will note set any environment that you need. So, there is 2 ways to do so,

1. Always execute the .profile or set the appropriate variables in your script.

OR

2. When submit the job in cron, you must type
* * * * * (. /home//.profile; /yourpath/yourjob)

Regards,
Patrick