I've got a problem with cron on my server which i think is down to the environment or user that cron runs under.
My app is set up under a user called deploy
. The root folder for the app is /var/www/apps/akrotoski
, and the executable for the app, which i need to run in this cron task, lives in a subfolder called bin
and is called thoth
.
My cron task is thus, created by typing "crontab -e" while logged in as the deploy
user:
# m h dom mon dow command
43 * * * * echo "time = `date`, USER=$USER, current folder = `pwd`, which thoth = `which thoth`" >> ~/import_times.txt; cd /var/www/apps/akrotoski; /var/www/apps/akrotoski/bin/thoth --run_command 'ExternalPost.import'
At the moment it's set up to run every hour, at 43 minutes past, so i can set it to a time that's just about to happen, then wait for it to kick off.
The first part, with the "echo" is just me trying to debug what's going on. If i run this on my command line it writes the following into ~/import_times.txt
:
time = Tue Nov 1 23:43:52 UTC 2011, USER=deploy, current folder = /var/www/apps/akrotoski, which thoth = /var/www/apps/akrotoski/bin/thoth
and then runs the thoth command (which imports some data off the web).
So, that all seems fine - it's got the right folder, the right user, the right exe. However, the line written into ~/import_times.txt
by the cron task looks like this:
time = Tue Nov 1 23:43:01 UTC 2011, USER=, current folder = /home/deploy, which thoth =
Ie, no user, which would explain in turn why which thoth
doesn't return anything since it gets the location out of the deploy (or root) user's PATH variable. Looking in my db, i can see that nothing's been imported, so my "thoth --run_command" call didn't get done properly.
If i create a crontab with "crontab -e", logged in as deploy, doesn't that run under deploy? Can someone set me straight as to what might be going wrong?
thanks, max
EDIT - for @ott below, who suggested i output the value of set - this is what i got from calling set in cron:
HOME='/home/deploy'
IFS='
'
LOGNAME='deploy'
OPTIND='1'
PATH='/usr/bin:/bin'
PPID='5948'
PS1='$ '
PS2='> '
PS4='+ '
PWD='/home/deploy'
SHELL='/bin/sh'
The path is much reduced, but i do pass the full path to the thoth exe in my cron job anyway:
cd /var/www/apps/akrotoski; /var/www/apps/akrotoski/bin/thoth --run_command 'ExternalPost.import'
Can you see anything missing from set
which would be required to make this work?
EDIT - i worked out the answer thanks to @ott's help, detailed in the comments below. It's below the comment fold though so for the sake of anyone else who's browsed to this, the problem was that the environment cron was running in knew about the ruby bin, but it didn't know that it had to open the thoth exe with ruby. So i just had to add 'ruby' before the thoth exe, with the full path to the ruby bin just to be on the safe side. I'm still cd'ing to the app folder first, again just to be safe, with all the full paths it might not be necessary:
0 * * * * cd /var/www/apps/akrotoski; /usr/local/bin/ruby /var/www/apps/akrotoski/bin/thoth --run_command 'ExternalPost.import'
thoth
program is executable by thedeploy
user.thoth
a binary or just a shellscript? If it's binary and you have strace on the system, change your command tostrace -fvo thoth.out /var/www/apps/akrotoski/bin/thoth --run_command 'ExternalPost.import'
. This gives a longer textfile, you could put it to pastebin.com or similar if you don't find the reason by yourself.