Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

crontab environment problem

211 views
Skip to first unread message

Russ P.

unread,
May 6, 2010, 2:10:22 PM5/6/10
to
I wrote a small csh script (on Linux) to run "make" (compile a
program). It works fine when I run it from the command line, but when
I try to run it with crontab, it doesn't work. I set the PATH variable
in the crontab file so it can find the script, but other environment
variables are apparently missing. How can I set the environment within
crontab to be exactly the same environment I have at the command line?
Thanks.

Russ P.

Bill Marcum

unread,
May 6, 2010, 2:54:24 PM5/6/10
to
Include commands to set the environment variables in your script, or
source your .login or .cshrc files.


--
[It is] best to confuse only one issue at a time.
-- K&R

Kenny McCormack

unread,
May 6, 2010, 3:11:14 PM5/6/10
to
In article <25a68589-0f29-4950...@h11g2000yqj.googlegroups.com>,

(Some people will think this solution overdone, and will suggest smaller
little hacks - like, oh, set this, set that - but the fact is the only
good way to do it that really does ensure that you get the same
environment in crontab as you would logging in)

Setup the cron job to run an Expect script, that ssh's into the box
(using private key authentication so that no password is necessary),
then runs your script in the logged in session. I did this a long time
ago, and it worked out real well. Took a while to get all the pieces
working right (redirecting all of the stdio to /dev/null, so I didn't
get an email from every job). Nice also, because Expect provides
logging (as a built-in feature), so you get a nice log of your session,
almost for free.

--
> No, I haven't, that's why I'm asking questions. If you won't help me,
> why don't you just go find your lost manhood elsewhere.

CLC in a nutshell.

Keith Keller

unread,
May 6, 2010, 3:54:29 PM5/6/10
to
On 2010-05-06, Kenny McCormack <gaz...@shell.xmission.com> wrote:
>
> (Some people will think this solution overdone, and will suggest smaller
> little hacks - like, oh, set this, set that - but the fact is the only
> good way to do it that really does ensure that you get the same
> environment in crontab as you would logging in)
>
> Setup the cron job to run an Expect script, that ssh's into the box

You're right--that is overdone. Bill's suggestion to source the
appropriate login file(s) is much cleaner, and really does ensure that
you get a login environment, without having to learn Expect or configure
ssh keys.

--keith


--
kkeller...@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information

Russ P.

unread,
May 6, 2010, 6:03:19 PM5/6/10
to
On May 6, 11:54 am, Bill Marcum <marcumb...@bellsouth.net> wrote:
> On 2010-05-06, Russ P. <russ.paie...@gmail.com> wrote:> I wrote a small csh script (on Linux) to run "make" (compile a

I tried that, but I can't seem to get it to load. I put

source ~/.cshrc

in the crontab file, but I got this reply:

crontab: installing new crontab
"/tmp/crontab.XXXXCGDs5E":4: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? y

What is the proper syntax? Thanks.

Russ P.

Message has been deleted

Russ P.

unread,
May 6, 2010, 7:57:29 PM5/6/10
to
On May 6, 4:16 pm, Michael Vilain <vil...@NOspamcop.net> wrote:
> In article
> <e3c1d24c-3442-4786-b711-c3276dbf9...@r11g2000yqa.googlegroups.com>,
> Most crond run the programs using sh by default, not csh.  I seem to
> recall you can specify the shell used but I may be wrong there.  Rewrite
> your script to use sh.  Not bash or ksh.  sh.  Yes, I know the default
> shell on some systems is bash, but if you write the lowest common
> denominator, you know it will run everywhere.
>
> Don't know sh.  Learn it and why it's the default for shell scripting
> rather than csh.
>
> --
> DeeDee, don't press that button!  DeeDee!  NO!  Dee...
> [I filter all Goggle Groups posts, so any reply may be automatically ignored]

Thanks for the suggestion, but I don't that is an option here. The
program I am trying to compile is heavily embedded in a csh
environment, and that is what I need to use.

I normally use bash, but in this case I am stuck with csh.

Russ P.

Keith Keller

unread,
May 6, 2010, 8:09:29 PM5/6/10
to
On 2010-05-06, Russ P. <russ.p...@gmail.com> wrote:
>
> I tried that, but I can't seem to get it to load. I put
>
> source ~/.cshrc
>
> in the crontab file, but I got this reply:

Don't put it in your crontab. Write a script wrapper that does what you
want done, and call that from crontab. You can even test your script
from a nonlogin shell (which will emulate the crontab environment,
though not completely).

Seebs

unread,
May 6, 2010, 8:16:28 PM5/6/10
to
On 2010-05-06, Russ P. <russ.p...@gmail.com> wrote:
> Thanks for the suggestion, but I don't that is an option here. The
> program I am trying to compile is heavily embedded in a csh
> environment, and that is what I need to use.

I am really confused.

You're *compiling* something in a crontab?

And the compilation is somehow able to depend on csh?

This sounds like a disaster-in-waiting to begin with. In any event, try
running "env" at a prompt (redirected to a file), then nuke any lines you're
sure don't matter, change the rest from
FOO=bar
to
setenv FOO bar
and append it to the script in question. That may do it.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Russ P.

unread,
May 6, 2010, 8:41:40 PM5/6/10
to
On May 6, 5:16 pm, Seebs <usenet-nos...@seebs.net> wrote:

> On 2010-05-06, Russ P. <russ.paie...@gmail.com> wrote:
>
> > Thanks for the suggestion, but I don't that is an option here. The
> > program I am trying to compile is heavily embedded in a csh
> > environment, and that is what I need to use.
>
> I am really confused.
>
> You're *compiling* something in a crontab?

What is confusing about that? It seems to me like a rather
straightforward thing to do.

Here's my situation. I am writing software that needs to communicate
through a socket with another large program. The other large program
is maintained in ClearCase, and I need to be able to compile it in my
ClearCase "view." That takes something like 10 to 20 minutes. So
rather than sit and wait for it, I figure I'll just have a cron job do
it for me in the middle of the night every night. Now, this large
program apparently depends on may environment variables that were set
up using csh. We have a "standard" cshrc file that others use, but I
use bash myself.

I took Keith Keller's suggestion to source .cshrc in the compilation
startup script, but that didn't solve my problem. I'm about ready to
just say to hell with it and just compile it from the command line.
Something is f***ed up -- maybe me. In any case, I certainly
appreciate the suggestions that were offered here.

Russ P.

> And the compilation is somehow able to depend on csh?
>
> This sounds like a disaster-in-waiting to begin with.  In any event, try
> running "env" at a prompt (redirected to a file), then nuke any lines you're
> sure don't matter, change the rest from
>         FOO=bar
> to
>         setenv FOO bar
> and append it to the script in question.  That may do it.
>
> -s
> --

> Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!

Seebs

unread,
May 6, 2010, 9:03:00 PM5/6/10
to
On 2010-05-07, Russ P. <russ.p...@gmail.com> wrote:
> On May 6, 5:16�pm, Seebs <usenet-nos...@seebs.net> wrote:
>> On 2010-05-06, Russ P. <russ.paie...@gmail.com> wrote:
>>
>> > Thanks for the suggestion, but I don't that is an option here. The
>> > program I am trying to compile is heavily embedded in a csh
>> > environment, and that is what I need to use.
>>
>> I am really confused.
>>
>> You're *compiling* something in a crontab?

> What is confusing about that? It seems to me like a rather
> straightforward thing to do.

I guess automated builds exist, but it seems odd. Normally I use crontabs
for regular maintenance tasks, like *running* programs, but I build them
with a person there to watch the build.

Unless the source is changing, why compile it repeatedly?

> Here's my situation. I am writing software that needs to communicate
> through a socket with another large program. The other large program
> is maintained in ClearCase, and I need to be able to compile it in my
> ClearCase "view." That takes something like 10 to 20 minutes. So
> rather than sit and wait for it, I figure I'll just have a cron job do
> it for me in the middle of the night every night.

Ahhh!

> Now, this large
> program apparently depends on may environment variables that were set
> up using csh. We have a "standard" cshrc file that others use, but I
> use bash myself.

Okay.

Here's what I'd probably do:

#!/bin/sh
csh -c 'source /standard/cshrc; cd /path/to/source; make'

> I took Keith Keller's suggestion to source .cshrc in the compilation
> startup script, but that didn't solve my problem. I'm about ready to
> just say to hell with it and just compile it from the command line.
> Something is f***ed up -- maybe me. In any case, I certainly
> appreciate the suggestions that were offered here.

I'd mess around with env. Run a cron job to the effect of:
env | mail -s "cron environment" $yourname
and compare it with running 'env' in an environment where the build
succeeds.

-s
--

Message has been deleted

Ben Bacarisse

unread,
May 6, 2010, 11:52:00 PM5/6/10
to
"Russ P." <russ.p...@gmail.com> writes:

> On May 6, 11:54 am, Bill Marcum <marcumb...@bellsouth.net> wrote:

<snip>


>> Include commands to set the environment variables in your script, or
>> source your .login or .cshrc files.

<snip>


> I tried that, but I can't seem to get it to load. I put
>
> source ~/.cshrc
>
> in the crontab file, but I got this reply:
>
> crontab: installing new crontab
> "/tmp/crontab.XXXXCGDs5E":4: bad minute
> errors in crontab file, can't install.
> Do you want to retry the same edit? y
>
> What is the proper syntax? Thanks.

The error message say that something is wrong with the minute field --
that's first one. I suspect you have an entry in the crontab file that
spans more than one line. This is not usually permitted (though there
are several of different cron implementations).

When the action is anything more than a simple command, it's usually
better to put the whole thing in a script and run that:

0 3 * * * /home/you/bin/my-compile-script

--
Ben.

Jonathan de Boyne Pollard

unread,
May 10, 2010, 4:53:04 PM5/10/10
to
>
>>
>> crontab: installing new crontab
>> "/tmp/crontab.XXXXCGDs5E":4: bad minute
>> errors in crontab file, can't install.
>> Do you want to retry the same edit? y
>>
> Most crond run the programs using sh by default, not csh. [...]
>
If you read it carefully, you'll find that the choice of shells for
running the script is nothing to do with the cause of the aforementioned
error message. In fact, the frequently given answer to the above
problem relates to a quite different fervently made software choice.
It's not, however, the answer in this particular case, as can be deduced
from the following:

> I put
>
> source ~/.cshrc
>
> in the crontab file [...]
>
The original instruction was, of course:

> Include commands to set the environment variables in your script [...]
>

Message has been deleted
0 new messages