Link to home
Start Free TrialLog in
Avatar of aleyva
aleyva

asked on

csh environment with cron

I have a perl script that runs fine when I am logged in as the user that will run the script. When I try to envoke it in that users cron, I have problems because apparently the startup files aren't getting initiated in order for the environment to be set. The script runs some Oracle commands and therefore require several variables to be set. I want to keep the script as generic as possible so that I can port it to different machines without having hardcoded variables. I am running AIX 4.2.1 on an RS6000.

Help!
Avatar of ecw
ecw

Don't invoke the perl script directly from cron, call a little csh wrapper that sources the users .login and then calls the perl script.  eg.

#!/usr/bin/csh

source $HOME/.login
exec perlscript
Avatar of aleyva

ASKER

Although this might work, Can you provide me some documentation as to why this doesn't work, and/or why?

I increased the points.
Avatar of aleyva

ASKER

Although this might work, Can you provide me some documentation as to why this doesn't work, and/or why?

I increased the points.
Avatar of ozo
why what doesn't work?
Avatar of aleyva

ASKER

I want to know why the environment doesn't get set when a cron job envokes a perl program?
Cron initialise a very small environment.  If I remember, it's
 SHELL=/bin/sh
 HOME=<users home dir>
 PATH=/bin:/usr/bin
 LOGNAME=<users login name>
Be careful, ecw's suggestion may work or not !!

As ecw said cron sets up a minimal environment.
If you want to have your users environment you should setup a
script as follows:

  #!/bin/csh -f -b
  su - user -c '/full/path/to/perlscript'
  exit 0

This calls any required and/or default startup (resource) scripts
for your login shell (as specified in /etc/passwd). These resource configuration scripts widly differ across various platforms (AIX, HP-UX, IRIX, Linux, etc.) and shells (sh, csh, bash, tcsh, etc.), they also may call system-wide files like
  /etc/profile
  /etc/csh.login
  /etc/csh.cshrc

Make shure that none of these (used) resource files make any output (stdout, stderr) and/or call programs modifying the tty (like stty, tset, etc.); otherwise your script, and therefore
cron, will fail.


This will work from roots crontab.  A non-root user can't su without a password.
Any terminal specfic operations on default stdin, stdout or stderr performed by commands invoked by cron will fail, but they may not be needed to successfuly complete the command.  Any input from stdin will return EOF, and any output to stdout or stderr is mailed by cron to the invoking id.  This is not neccessarily a failure.

The obvious precaution, is to re-direct stdin, stdout and stderr either in crontab or near the start of the cron invoked command.  And move application specific config such as environment variables into a dot file sourced at login, and by in-house commands.
Using  su  inside a script called by cron, I prerequested that this cron is called by the appropriate user, then  "su -"  won't
ask for password.

Any terminal oparation in the script may stop the script immediately (my experiance from SunOS, early Solaris).

No offence, just sharing knowledge ;-)
Avatar of aleyva

ASKER

The reason is because the solution requires execution of the cron job as root and therefore not acceptable. At this point ecw has offered a working solution that doesn't impose on root. I was under the impression that the environment of the user calling the script would be established even though the script/program is called by cron. Apparently not but the wrapper does work for now. I would still like to know of any documentation that offers a different solution. I think that 200 points warrents a complete answer if possible. Thanx for all your efforts ahoffmann!
I regret to say that cron never includes a full environment in invoked jobs, and I doubt if it ever will.  If you were to invoke the commands with at(1), you'll find it does include the full environment, including the current working directory, at the time at(1) was run.  You could hack a repeated job with at(1) by making it re-submit itself each time it's run.

In my opinion, the only reliable way for you to do what you want, is for you perl script to build its own environment from a file stored in a well know place.  On each machine/system you copy the script to, dump a working environment into this well know file using env(1), and at the start of your script include something like:

$env_config = "$ENV{HOME}/.myscript.cfg";
open(ENV, "<$env_config") || die "$0: $env_config - $!\n";
while (<ENV>) {
  chop;
  ($var, $val) = split(/=/, $_, 2);
  $ENV{$var} = $val;
}
close(ENV);
So I post it as comment 'til it satisfies ;-)


#!/bin/csh -f -b
# called by root's crontab for user 'name'
onintr _abort
set yourname=name
set userhome=`awk -F: /^$username/'{print $(NF-1)}' /etc/passwd`
source /etc/csh.cshrc      # or whatever your systemwide file is
source /etc/csh.login      # dito
source $userhome/.cshrc
source $userhome/.login
perlscript >& $userhome/perlscript.log
exit 0
_abort:
echo "${0}: got unexpected signal $status; exiting abnormaly" \
      >> $userhome/perlscript.log
exit 1

Avatar of aleyva

ASKER

At this point, the script is working as suggested originally by ecw, but I like some of what ahoffmann has written. I would like to split the points between the two of you. Thanx to the both of you for your time and effort.

How do I proceed?
I don't know any other way than posting another question to grade more than one person, but you may have a look at the Experts-Exchange topic and probably ask there.
Avatar of aleyva

ASKER

I have asked the question to Experts-Exchange, and will reward both of you. Whoever posts an answer to this one will get the first 200 points, the other will get a new question posted for the their 200 points. Again, I thank you both for your efforts. I have definitely learned something with these answers.

Angel
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aleyva

ASKER

I apologize for the F grade before.
A