Why is the cron ENV different from the user's ENV?

Solution 1:

Regarding the question - why is this so - the manual page that explains it is crontab(5), IOW the one accessible through man 5 crontab (not the default one in section 1). The cron daemon does not try to emulate a shell session, rather it sets up a clean, minimal environment for the cron jobs to run in, and then in turn allows the crontab file to set its own arbitrary environment variables. The newer cron daemon shipped with Debian also has several additional provisions for pam_env etc.

Solution 2:

Cron does not execute processes in a login shell. Because of this, all the typical scripts are not sourced when a process is executed.

Executing the process from within a login shell should replicate the user's environment.

Put something like this in a crontab and compare the two outputs:

*/1 * * * * /usr/bin/env > /tmp/env                                             
*/1 * * * * /usr/bin/bash -l -c /usr/bin/env > /tmp/bashenv  

As you can see, /tmp/bashenv will have a whole slew of environment variables that /tmp/env does not. This is because env was invoked in a login shell using bash -l.