Summary: Cron & .profile

From: Joe Dietz (jdietz@mis.uswest.com)
Date: Fri Feb 24 1995 - 00:21:09 CST


ORIGINAL QUESTION:
==================

I have a problem with cron on SunOS 4.1.3_U1 on a sparc20. We have a script
that runs fine--if run interactively by the user. In attempts to "cron" the
script the cron entry kicks off the script ok but we get errors. Some testing
shows that cron is running the script in "sh" and as the desired userid...but...
the environment variables like "path" are not getting set.

It would appear that cron is not reading the .profile before it runs the script.
Or cron is running the .profile and the environment is not being passed to the
script. Switching to different shells did not help...

I have another SA under SCO who has seen the same problem...There must be
something in comman with the way these scripts are build...or is it some detail
of cron we are missing...

tried
00 00 * * * /bin/sh /script.sh
and
00 00 * * * /script.sh

Any and all ideas and comments welcome...thanks
joe

==============
ANSWER:
==============
The facts are that cron does not read any shell startup files like .cshrc,
.profile, or .login. It is necessary to manually call this startup files from
cron or the script being croned. Others suggested setting all path information
explicitly in the script--the solution we went with. Here are a few examples
folks sent. Understand that I did not test all of these options.

OPTIONS:
1)
2 4 * * * (. $HOME/.profile; my_script.sh)

2)
from the man pages on crontab(5) (i.e. "man 5 crontab"):

     The command is run from your home directory with an arg0 of
     sh. Users who desire to have their .profile executed must
     explicitly do so in the command. cron supplies a default
     environment for every shell, defining HOME, LOGNAME, USER,
     SHELL(=/bin/sh), and PATH(=:/usr/ucb:/bin:/usr/bin).

3)
 su - user -c /path_to_script/script.sh

4)
I would recommend doing a ". $HOME/.profile" near the beginning of the
script or at least setting some minimal set of vars that are necessary.

5)
A workaround was to make a shell wrapper which did two things...
1. Source the ".profile" or ".cshrc".
        NOTE : You must specify the home directory.
2. Start the script you want to execute.

Then cron the shell wrapper instead...

I guess a generic wrapper could be something like this:
vi /read_path
----------------------
#!/bin/sh
. /.profile
$@
----------------------
Then in cron you could have

0 12 * * * /read_path <executable_originally_in_cron>

6)
00 00 * * * . /etc/profile; . .profile; script.sh

Thanks to all who replied....sorry if I missed anyone

seanw@amgen.com (Sean Ward)
etnibsd!vsh@uunet.uu.net (Steve Harris)
ericb@telecnnct.com (Eric William Burger)
sozoa@atmel.com (Steve Ozoa)
mike@trdlnk.com (Michael Sullivan)
z055084@uprc.com (Kohler R. P. (Robert))
Kambiz Aghaiepour <kxaghai@srv.pacbell.com>
dfalk@sqwest.bc.ca
Tom Orban <orban@advtech.uswest.com>
Bob Mende Pie <mende@pieinthesky.rutgers.edu>
bshaw@soleil.spdc.ti.com (Bob Shaw)
Neil Greene <neil@bmd.com>
citicds!cntower!arash@uunet.uu.net (Arash Jahangir)
Robert Sargent <sargent@sgt.com>
tkevans@eplrx7.es.dupont.com (Tim Evans)
terrym@meaddata.com (Terry Miesse)
Rex McMaster <rmcm@ariel.ucs.unimelb.edu.au>
met@leis.leis.bellcore.com (met)
bismark@alta.jpl.nasa.gov (Bismark Espinoza)
perryh@pluto.rain.com (Perry Hutchison)
david@srv.pacbell.com (David St. Pierre)



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:10:17 CDT