Operating System - HP-UX
1825050 Members
3017 Online
109678 Solutions
New Discussion юеВ

User environment for cron jobs

 
dictum9
Super Advisor

User environment for cron jobs


How do I control the environment when I run cron jobs, in other words what startup file is getting sourced in? $HOME/.login? I doubt $HOME/.profile is getting sourced in.
7 REPLIES 7
Mark Greene_1
Honored Contributor

Re: User environment for cron jobs

You are right, it is not. You have to explicitly set the environment. Cron does not go through logind so none of the usual events that happen during login happen in cron.

mark
the future will be a lot like now, only later
Pete Randall
Outstanding Contributor

Re: User environment for cron jobs

"what startup file is getting sourced in?"

Answer: none

Cron runs with a minimal evironment. Check the man page for crontab if you want to know the details. Your best bet is to source a profile that will set the PATH and whatever environment variables you will need. You could source /etc/profile or $HOME/.profile or some other profile that combines these to give you what you need.

You could also explicitly define the variables and PATH in your cron job - whichever works for you.


Pete

Pete
Carlos Roberto Schimidt
Regular Advisor

Re: User environment for cron jobs

If you dont want explicitly set the environment, is possible you put in first line from script to run in cron:

. $HOME/.profile

But you have to set HOME variable.

Schimidt
Pete Randall
Outstanding Contributor

Re: User environment for cron jobs

Actually, to quote the man page:

"cron supplies a default environment for every shell, defining:

HOME=user's-home-directory
LOGNAME=user's-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry."


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: User environment for cron jobs

Hi:

The 'cron' environment is quite sparse as the manpages for 'crontab' will divulge. For example, your PATH consists only of '/usr/bin' and '/usr/sbin'.

The most appropriate way, in my opinion, to define commonly used environmental variables is to collect them in a standalone file that can be sourced (read) *either* by your '$HOME/.profile' as it is processed during login() or by scripts that you run and/or cron.

Regards!

...JRF...
Arunvijai_4
Honored Contributor

Re: User environment for cron jobs

Hi etc,

"cron" doesnt take any ENV variables. You need to set them individually,

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
A. Clay Stephenson
Acclaimed Contributor

Re: User environment for cron jobs

As has been mentioned cron has an intentionally sparse environment. Generally, it is not a good idea to source a user's .profile within a cron'ed script. Why? Because the default .profile's under HP-UX contain commands which extect stdin and stdout to be a tty device (i.e. a terminal) and can hang if those conditions are not met (e.g. under cron).

The least evil approach I have found is to setup a separate file to set and export any needed variables, e.g. /usr/local/bin/myenv.sh, and have the user's .profile AND your cron'ed script source this file via the . (dot) operator.
This sourced file must not contain any return or exit statements as that will have the effect of terminating the process.
If it ain't broke, I can fix that.