Solved

csh environment with cron

Posted on 1998-08-18
17
606 Views
Last Modified: 2012-05-04
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!
0
Comment
Question by:aleyva
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 5

Expert Comment

by:ecw
Comment Utility
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
0
 

Author Comment

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

I increased the points.
0
 

Author Comment

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

I increased the points.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
why what doesn't work?
0
 

Author Comment

by:aleyva
Comment Utility
I want to know why the environment doesn't get set when a cron job envokes a perl program?
0
 
LVL 5

Expert Comment

by:ecw
Comment Utility
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>
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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.


0
 
LVL 5

Expert Comment

by:ecw
Comment Utility
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.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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 ;-)
0
 

Author Comment

by:aleyva
Comment Utility
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!
0
 
LVL 5

Expert Comment

by:ecw
Comment Utility
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);
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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

0
 

Author Comment

by:aleyva
Comment Utility
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?
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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.
0
 

Author Comment

by:aleyva
Comment Utility
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
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 total points
Comment Utility
> I have definitely learned something with these answers.
That's what this game is for :-))
0
 

Author Comment

by:aleyva
Comment Utility
I apologize for the F grade before.
A
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Input from stdin for perl 6 108
transpose into pipe delemited 8 65
Extract data from span tag 1 87
PERL - Find newest folder 12 100
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now