2

I've created the following cronjob to run every 5 minutes, but for some reason it doesn't.

*/05 * * * * ~/webapps/django/shop/update

When I try to run the script from the shell, it runs perfectly. The contents of the script (trying to run a custom django command) are

python2.7 manage.py updateTime

Any ideas as to what could be wrong?

Thanks.

EDIT:

The shell environment variables:

BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
BASH_VERSION='3.2.25(1)-release'
COLORS=/etc/DIR_COLORS.xterm
COLUMNS=157
CVS_RSH=ssh
DIRSTACK=()
EDITOR=emacs
EUID=629
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/home/shopperspoll/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HISTTIMEFORMAT='%F %T '
HOME=/home/shopperspoll
HOSTNAME=web192.webfaction.com
HOSTTYPE=i686
IFS=$' \t\n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=46
LOGNAME=shopperspoll
LS_COLORS='no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:'
MACHTYPE=i686-redhat-linux-gnu
MAIL=/var/spool/mail/shopperspoll
MAILCHECK=60
OLDPWD=/home/shopperspoll
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/shopperspoll/bin
PIPESTATUS=([0]="0")
PPID=18520
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/home/shopperspoll
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=1
SSH_CLIENT='99.65.178.55 59982 22'
SSH_CONNECTION='99.65.178.55 59982 174.133.20.142 22'
SSH_TTY=/dev/pts/11
TERM=xterm
UID=629
USER=shopperspoll
_=test
consoletype=pty
tmpid=629

The cron environment variables:

BASH=/bin/sh
BASH_ARGC=()
BASH_ARGV=()
BASH_EXECUTION_STRING='set >/home/shopperspoll/test'
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
BASH_VERSION='3.2.25(1)-release'
DIRSTACK=()
EUID=629
GROUPS=()
HOME=/home/shopperspoll
HOSTNAME=web192.webfaction.com
HOSTTYPE=i686
IFS='
'
LOGNAME=shopperspoll
MACHTYPE=i686-redhat-linux-gnu
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/bin:/bin
POSIXLY_CORRECT=y
PPID=24949
PS4='+ '
PWD=/home/shopperspoll
SHELL=/bin/sh
SHELLOPTS=braceexpand:hashall:interactive-comments:posix
SHLVL=1
TERM=dumb
UID=629
USER=shopperspoll
_=/bin/sh
iman453
  • 9,105
  • 16
  • 54
  • 70

3 Answers3

4

First steps first: replace the cron entry with:

*/05 * * * * date >/tmp/qqdate

and see that it is actually running by checking that qqdate gets created/updated.

Then, try using the complete path name to your update script (no ~ characters) - the tilde is a shell thing that may not be understood by cron.

Beyond that, most cron problems where it works from the shell but not within cron are usually to do with the greatly minimised environment that cronjobs get.

Execute a set in your shell and one withing the cronjob and work out the differences. It's likely that the cronjob is missing a vital environment variable.


What I normally do is take that shell variable file (from a set >shellvarfile), put an export before every line and source that (with . /path/to/shellvarfile) from my cronjob script. If that fixes it, then I know it's an environment problem and it's a matter of commenting out those lines one by one until it breaks again. The last one you comment out is a needed one so uncomment it and mark it so. Then keep going until as many as possible are commented out.

Failing that, try to work out why it's failing. Change the cronjob to something like:

*/05 * * * * /path/to/webapps/django/shop/update >/tmp/debug 2>&1

Then wait for it to run and check the contents of that file for clues. Assuming that the cronjob is running (as you state it is), this should capture the output and hopefully indicate what the problem is. Make sure you post the contents of that file.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thank you for your reply (and sorry for the late response). The date cronjob seems to work. And replacing the ~ didn't fix it either. I've edited my question and pasted the environment variables for my shell and cron environment. There are quite a few missing variables in the corn environment and I'm not sure which one is needed. – iman453 May 24 '11 at 00:19
  • @iman453, what I normally do is take that shell variable file, put an `export ` before every line and source that from my cronjob. If that fixes it, then I know it's an environment problem and it's a matter of commenting out those lines one by one until it breaks again. The last one you comment out is a needed one so uncomment it and mark it so. Then keep going until as many as possible are commented out. – paxdiablo May 24 '11 at 01:26
  • It's really weird but it still doesn't seem to work. I've done an export before every variable and pasted the whole thing in my script being called by cron. That should do the trick right? – iman453 May 24 '11 at 08:33
  • @iman453: yes, _if_ it's an environment problem. I've added that suggestion to the answer, including another thing to try. – paxdiablo May 24 '11 at 08:38
  • Thank you! It was not able to find python, and so I appended the appropriate path to the PATH variable. Thanks again. – iman453 May 24 '11 at 08:58
2

Just to speed up without having to read through all those comments. I was facing the same problem, which is that the cronjob was not finding the Python executable. So I looked for it:

whereis python2.7

With the location, I've edited my crontab:

crontab -e

And finally, my task was looking like this:

0 */6 * * * /usr/local/bin/python2.7 ~/webapps/proj/app/manage.py poll_twitter

Works like a charm now.

aldux
  • 2,774
  • 2
  • 25
  • 36
1

According to the logic in your sript, your manage.py is located in the home directory of a user which runs cron. I doubt that this is true.

Adjust you cron task like this:

*/05 * * * * python2.7 /absolute/path/to/manage.py updateTime

This should fix the issue.

Silver Light
  • 44,202
  • 36
  • 123
  • 164