Operating System - HP-UX
1846827 Members
9622 Online
110256 Solutions
New Discussion

sourcing /etc/profile and .profile in cronjobs

 
SOLVED
Go to solution
Jeganathan Venkitasamy_3
Occasional Contributor

sourcing /etc/profile and .profile in cronjobs

Hi,

In HP-UX 11i, is there any simple and standard way to source .profile and /etc/profile in a cronjob than just explicitly add lines
" . $HOME/.profile
. /etc/profile" in the cronjob scripts?

We are running HP-UX 11.00 ( we will be moving to 11i soon) and we do explicitly call /etc/profile & .profile in cron job scripts. But I think this is not a nice method. Any better ways?

Thanks & regards,
Jegi
5 REPLIES 5
James Specht
Trusted Contributor

Re: sourcing /etc/profile and .profile in cronjobs

I have an 11i box and this is quoted from the man of crontab.

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

I'll follow the thread for a while to see if anyone has a way, because I do the same thing when needed.

--Jim
"Everyone can be taught to sculpt: Michelangelo would have had to be taught how not to. So it is with the great programmers."
Pete Randall
Outstanding Contributor

Re: sourcing /etc/profile and .profile in cronjobs

Jegi,

That's the only way I know of.



Pete


Pete
Jeff Schussele
Honored Contributor
Solution

Re: sourcing /etc/profile and .profile in cronjobs

Hi Jegi,

Although outright sourcing *will* work the majority of the time, you have to be careful about routines in the profiles that are looking for a terminal. In these cases where there is NO terminal, you'll get the messages like:

process not attached to terminal
Usage: who [-rbtpludAasHTqRm] [am i] [utmp_like_file]

r run level
b boot time
t time changes
p processes other than getty or users
l login processes
u useful information
d dead processes
A accounting information
a all (rbtpludA options)
s short form of who (no time since last output or pid)
H print header
T status of tty (+ writable, - not writable, x exclusive open, ? hung)
q quick who
R print host name
ttytype: couldn't open /dev/tty for reading
stty: : Not a typewriter

Now normally this isn't a big problem, but it can be avoided by creating a file to source all the necessary environment variables from without ANY code or routines that might be looking for a terminal.
Then you can just source that file in the profile & .profile files as well as the beginning of any cron job scripts.

My $0.02,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
A. Clay Stephenson
Acclaimed Contributor

Re: sourcing /etc/profile and .profile in cronjobs

This is something that you really don't want to do. The problem is that almost all .profile's contain commands like tset, tput, tabs which expect an interactive environment --- which you ain't. You could surround all those commands with
if [[ -t 0 ]]
then
tput
tset
...
fi

but the far better way is to create a file which sets and exports any needed variables, e.g. /usr/local/bin/myenv.sh and then both your .profile and cron scripts source this file via the dot command. This sources file must not contain an exit or return statement or it will have the unintended effect of exiting the foreground (shell) process.
If it ain't broke, I can fix that.
Pete Randall
Outstanding Contributor

Re: sourcing /etc/profile and .profile in cronjobs

In light of the more recent responses, my initial post looks a trifle simplistic - I'll qualify my answer:

The only way I know of to ensure that all required environment variables are set in cron jobs is to either code them in the script, or source a file containing the required variables. This *could* be /etc/profile or .profile, but you would be better off to create a separate script containing just the variables required.


Pete


Pete