Often you will find yourself in the situation that one of your cron jobs works perfectly if you execute it manually but fails as soon as is gets executed by cron. The root cause might be the different environment. To simulate the environment of a cron job add the following entry to your crontab using
This will create the file
cronenv in your root directory and dump all available environment variables into it. Once the file was created remove the entry from your crontab and take a look at its contents:
HOME=/root LOGNAME=root PATH=/usr/bin:/bin LANG=en_US.UTF-8 SHELL=/bin/sh PWD=/root
This pretty much sums up the environment. To enter this environment use the following command:
env - cat /root/cronenv
Now you can execute your cron job command, see what happens and fix your error. Once you’re done simply type
exit to leave the environment.
See this Stack Overflow answer for more information.