Take the 2-minute tour ×

This question already has an answer here:

I have the following shell script: /var/local/myapp/bin/import


# Move to base directory
cd $(dirname $0)
cd ..

# Execute and bail if one of them fails
cake compile
cake tms:fetch
cake tms:fetch:clean
cake tms:split
cake tms:injest:main
cake tms:injest:schedules

I'm trying to run it from cron. All users have access to cake. However, when I run this script with cron, I get the following error (on every cake line):

/var/local/myapp/bin/import: line 13: cake: command not found

Here's my crontab

* * * * * /var/local/myapp/bin/import

Also, I can't figure out an easy way to redirect the stdout of bin/import to a file when run from cron. Adding > /var/local/myapp/import.log to the end of the cron statement just truncates import.log - the only way I can do it is to add exec &> import.log to bin/import itself, which isn't what I want to do at all, because when I run it by hand I want to see the output on the screen.

Everything works perfectly when I just type /var/local/myapp/bin/import from my terminal.

share|improve this question

marked as duplicate by Iain, Jenny D, Tom O'Connor May 13 at 14:17


add comment

2 Answers

up vote 4 down vote accepted

The PATH for cron is different from that for a user. In your script specify the full directory for cake, for example:

/path/to/cake compile

If all you're getting right now is error messages, that's why your output file is truncated. Fixing the directory problem should make that work, too. If you also want to log the errors, you can do one of these:

* * * * * /var/local/myapp/bin/import > /var/local/myapp/import.log 2>&1


* * * * * /var/local/myapp/bin/import > /var/local/myapp/import.log 2> /var/local/myapp/error.log

And that's assuming you want it run every minute. If you want it run every hour on the half hour, you'd do something like this:

share|improve this answer
I'd also suggestion appending >> /var/log/myapp/import.log instead of overwriting > /var/log/myapp/import.log if you really do want to run it every minuite. –  Zypher Jul 18 '10 at 21:14
That makes sense (about missing stderr). I didn't think about the PATH. My new crontab is * * * * * PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin /var/local/myapp/bin/import >> /var/local/tvserver/import.log 2>&1 and it works! –  Sean Clark Hess Jul 18 '10 at 21:21
add comment

As you have found, cron runs with a restricted environment, including path.

Either set the PATH at the top of the file, or (better) specify the full /path/to/cake (and /path/to/log).

>> means append to a file.

In your crontab, you should probably specify a time or times rather than go with all *s.

share|improve this answer
yeah, the *'s are just for testing. Thanks! –  Sean Clark Hess Jul 18 '10 at 21:15
Interestingly, since cake tries to use /usr/bin/env node, which also doesn't work, I had to use the PATH option. Thanks! –  Sean Clark Hess Jul 18 '10 at 21:22
add comment
