Today I learned why my cron task was not working.

There was a weird bug where I could run the script and it would work just fine, but when crontab ran the script, it wouldn’t work. I also knew crontab was working by running other more simple scripts.

Apparently, crontab has a different set of environment variables that it uses, compared to manually running a script. I tested by running env > /home/dan/env-cron.txt via crontab and running env > /home/dan/env-dan.txt myself. Here were my results:

# env-cron-root.txt
HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
LANG=en_US.UTF-8
SHELL=/bin/sh
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
PWD=/root

# env-cron-dan.txt
HOME=/home/dan
LOGNAME=dan
PATH=/usr/bin:/bin
LANG=en_US.UTF-8
SHELL=/bin/sh
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
PWD=/home/dan

# env-dan.txt
XDG_6SESSION_ID=526
TERM=xterm
SHELL=/bin/bash
SSH_CLIENT=555.555.555.555 55555 55
SSH_TTY=/dev/pts/0
USER=danial
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=$
MAIL=/var/mail/my-username
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PWD=/home/dan
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
LANG=en_US.UTF-8
NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
SHLVL=1
HOME=/home/dan
LOGNAME=dan
LESSOPEN=| /usr/bin/lesspipe %s
XDG_RUNTIME_DIR=/run/user/1001
LESSCLOSE=/usr/bin/lesspipe %s %s
_ =/usr/bin/env

Also, note.. regardless of script location, the working directory (pwd) starts off at the user home (~). My error was using relative locations from script location rather than home location.

More details on Stack Overflow: Reasons why crontab does not work