1

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

#!/bin/bash

# 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.

Sean Clark Hess
  • 263
  • 3
  • 13

2 Answers2

4

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

or

* * * * * /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:

30 * * * * /var/local/myapp/bin/import > /var/local/myapp/import.log 2>&1
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
  • 1
    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
1

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.

ramruma
  • 2,730
  • 1
  • 14
  • 8