I have a script that looks like this:


D=~/brew\ update

num=$(ls "$D" | cut -d ' ' -f 2 | sort -nr | head -1)
num=$(( num + 1 ))

script -q "$D/brew_update $num" brew update

This script always works, but when I start it with cron 0 */5 * * * ~/bin/brewupdate2, it says this in a file in /var/mail

^Dscript: brew: No such file or directory

So I thought it was that it uses sh, but I tried sh ~/bin/brewupdate2 and it ran without error.

 D=~/brew\ update
 # the above will never work unless you start script as you
 # say you are user fred, this full path will always work
 D=/home/fred/brew\ update

Cron works with different permissions and PATH than user, in other words cron basically is its own user (root), with its own PATH etc. Don't use ~/, that refers to another home directory, not yours, when cron starts the job, that ~/ is relative to the home directory of the user in the system that starts the job (root in this case). Use the full path for 'brew update' (and if you can, get rid of that directory name with the space in it, if it's under your control). I have no idea what cron even does with ~/ in terms of generating a path because it will never work predictably so I never thought about that.

With cron, ALWAYS use full system paths, or you will get these kinds of errors.

From serverfault:

what user will [cron] run as?

They all run as root. If you need otherwise, use su in the script or add a crontab entry to the user's crontab (man crontab) or the system-wide crontab (whose location I couldn't tell you on CentOS).

So in theory, you could use ~/ which would translate to the home of root, /root/, but that's a very bad idea re readability and testing etc.

[update]As noted, the issue here was 'brew' being in /usr/local/bin, which is not in $PATH Of cron, which made the command 'brew' fail, file not found. I missed that last line. But all the more reason to always use full paths. Full paths on all the programs and files will resolve all these issues.

  • Cron runs commands as a specific user just in a non-interactive shell so the environment may be completely different but ~ is perfectly valid, although discouraged. – David King Nov 10 '15 at 21:15
  • How do you make cron run as a specific user? I've never used that feature. In this case, for example, cron was clearly looking for a file in a home directory that did not exist, thus the error. For example, when cron starts a job, which user is it? root, by default? or cron? Since hard coding the paths etc in always makes it work, I've never explored the other cases. – Lizardx Nov 10 '15 at 21:17
  • @Lizardx The error doesn't seem to be caused by tilde (~) expansion leading to a file in $HOME not being found as you mentioned. Rather, the PATH environment variable is causing the error here. The command being supplied to script is: brew update. The brew command is typically located in /usr/local/bin on OS X based systems (Homebrew being a package manager for OS X). The brew: No such file or directory error is coming from script in this case, which would indicate that PATH is not set to reference /usr/local/bin. – soulcake Nov 10 '15 at 21:43
  • That makes sense, in my experience, the top reason I had to learn to start using full paths in cron jobs was because of BSD systems having the tool needed in /usr/local/bin or something like that, which was not in the $PATH of cron. – Lizardx Nov 10 '15 at 21:53
  • I'd missed the brew path thing, heh, I updated the answer to make it more complete. But all the more reason to always get in the habit of using full paths in all cron called scripts. – Lizardx Nov 10 '15 at 21:59

As has been mentioned in @Lizardx's answer, the PATH environment variable will likely differ from that of your user account. The error you are seeing indicates that script is unable to find the command brew:

brew: No such file or directory

The simplest way to remedy the error is to specify the exact path of the command fed to script:

script -q "$D/brew_update $num" /usr/local/bin/brew update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.