?

Log in

No account? Create an account

GEEK: crontab weirdness? - Open Knowledge — LiveJournal

Feb. 7th, 2007

02:19 pm - GEEK: crontab weirdness?

Previous Entry Share Next Entry

Okay, so I have this crontab:

SHELL=/bin/sh
PATH=/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/Users/build/bin:/sw/bin
HOME=/var/log
MAILTO=dev@example.com
MROOT=/Users/build
DATE=`date +%Y-%m-%d_%H%M`

# Nightly Processes
*      *      *       *       *     $MROOT/test.py >& $MROOT/log/testlog.$DATE


It's supposed to log the ouptut of test.py to $MROOT/log/testlog.$DATE every minute. However, although it creates the log file, the file is empty:

sh-2.05b# ls -al testlog*
-rw-r--r--   1 build  build  0 Feb  7 14:18 testlog.2007-02-07_1418


Now, I know that test.py is creating output--if I run it from the command line, I can see the output. And that output appears to be coming from stderr--if I run ./test.py > testlog.out, nothing is logged, but if run ./test.py 2> testlog.out, the output is logged.

I also know the ">&" redirect is C-shell syntax. So I tried it with Bourne shell syntax:

* * * * * $MROOT/test.py > $MROOT/log/testlog.$DATE 2>&1

This time, the output file was created (still empty). But now the logfile name instead of being set to this:

testlog.2007-02-07_1418

was set to this:

testlog.`date +%Y-%m-%d_%H%M`

Anyone know:

1) Why changing the redirect would cause the $DATE variable to be substituted with the string literal rather than the output of the date command?

2) Why stderr isn't being logged to the log file?

(I'm running on 10.4.8 Tiger Server).

Comments:

[User Picture]
From:chrisla
Date:February 7th, 2007 09:30 pm (UTC)

export

(Link)
Maybe try exporting $DATE ?

export $DATE

-Chris
(Reply) (Thread)
[User Picture]
From:ch
Date:February 8th, 2007 01:52 am (UTC)

Re: export

(Link)
nope. try again.
(Reply) (Parent) (Thread)
[User Picture]
From:crasch
Date:February 8th, 2007 07:01 pm (UTC)

Re: export

(Link)
Thanks for the suggestion.
(Reply) (Parent) (Thread)
[User Picture]
From:zanfur
Date:February 8th, 2007 12:12 am (UTC)
(Link)
I believe it's a shell issue. According to your crontab, you're using /bin/sh to parse the cron commands, and /bin/sh doesn't have the 2>&1 syntax (that's only bash, if I'm not mistaken). On some systems, /bin/sh and /bin/bash are the same file (often hardlinked), but will often behave differently depending on what name was used to invoke them. I'd try changing the shell to /bin/bash.

This also explains the date format issue. If /bin/sh *is* trying to emulate different shells, it may have "recognized" the different syntax and updated the date format string accordingly.
(Reply) (Thread)
[User Picture]
From:ch
Date:February 8th, 2007 01:52 am (UTC)
(Link)
> /bin/sh doesn't have the 2>&1 syntax (that's
> only bash, if I'm not mistaken)

ding! thank you for playing.

you're mistaken. that's honest-to-goodness, original Bourne shell syntax.
(Reply) (Parent) (Thread)
[User Picture]
From:zanfur
Date:February 8th, 2007 02:36 am (UTC)
(Link)
Ah, well, can't win them all.
(Reply) (Parent) (Thread)
[User Picture]
From:crasch
Date:February 8th, 2007 07:01 pm (UTC)
(Link)
Thanks for the suggestion.
(Reply) (Parent) (Thread)
[User Picture]
From:ch
Date:February 8th, 2007 01:51 am (UTC)
(Link)
cron handles the var settings by putting them literally in the environment. it doesn't do backquote processing.

if you move the backquote biz into the cmd on the crontab line, it will get handled by $SHELL and do what you want.

(Reply) (Thread)
[User Picture]
From:crasch
Date:February 8th, 2007 07:00 pm (UTC)
(Link)
Thanks!
(Reply) (Parent) (Thread)