I have a server running Ubuntu 12.04, which we use to host a Rails app. I'm running a task on cron, which is made up of a couple of unix commands separated by &&
- e.g:
cd /home/deploy/app/current && RAILS_ENV=production ...
The final bit has been snipped, as the command is failing on the very first cd
, with this:
cd: No such file or directory
In the deploy user's environment that cd works without a problem. The directory exists, and it's a symbolic link:
lrwxrwxrwx 1 deploy deploy 42 Mar 4 15:28 /home/deploy/app/current -> /home/deploy/app/releases/20130304152305
I thought it was strange that the ability for the shell to cd to a symbolic link would be affected by the environment, so I spit out the cron's environment variables to a file, according to this question: How to simulate the environment cron executes a script with?.
If I try and execute that line as the cron user, it fails:
$ env - $(cat ~/cron.env) cd /home/deploy/app/current
env: cd: No such file or directory
Why does this happen? The cron's environment variables are:
HOME=/home/deploy
LOGNAME=deploy
PATH=/usr/bin:/bin
LANG=en_US.UTF-8
SHELL=/bin/sh
PWD=/home/deploy
cd
is a shell builtin, not an program, and so neither cron
nor env
cannot execute it directly. The following should work for your cron job:
sh -c "cd /home/deploy/app/current && RAILS_ENV=production ..."