0

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'
10
  • 1
    I believe cron runs with an empty environment (no variables), unless they are specifically set by the script that is running. So either hard code them (e.g. for paths, use absolute paths) or export the variables needed in your script.
    – cyberx86
    Nov 2, 2011 at 0:16
  • thanks @cyberx86. I did hardcode the path to the exe (thoth) since i thought that might be the case, but it doesn't seem to run. I'll try putting some logging inside my app so i can see if it does anythin at all. Nov 2, 2011 at 9:21
  • Check (and post relevant parts of) your cron log for clues, and check that the thoth program is executable by the deploy user.
    – cyberx86
    Nov 2, 2011 at 11:49
  • Is thoth a binary or just a shellscript? If it's binary and you have strace on the system, change your command to strace -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.
    – ott--
    Nov 2, 2011 at 12:07
  • Thanks @ott. I've given strace a go. I can't see anything obviously wrong but i'm not sure what it all means. Here's the strace for the working version, when i call it normally from the command line: dl.dropbox.com/u/846812/thoth_working.out, and here's the not-working strace generated by calling it from cron: dl.dropbox.com/u/846812/thoth_not_working.out Nov 6, 2011 at 17:00

1 Answer 1

1

Modify your command to see the environment like this:

( set ; echo "time = `date`, USER=$USER, current folder = `pwd`, which thoth = `which thoth`" ) >> ~/import_times.txt

You'll notice a very reduced environment and PATH. You could extract the username from the HOME variable.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .