In Debian's manpage of cron,
The files in /etc/cron.d/ are independent of /etc/crontab: they do not, for example, inherit environment variable settings from it.
and from the LinuxQuestions.org Forum: /etc/crontab vs /etc/cron.d vs /var/spool/cron/crontabs/,
scripts that are in /etc/cron.d/ don't load environment variables.
I'm assuming you added your command as root in the /etc/crontab file. If that's the fact then executing the crontab line will load the user's environment variables which don't get loaded when you put the script in /etc/cron.d.
I was wondering what the sentences highlighted by me mean? What is the "inherit"ance from?
For
/etc/cron.d/*
, cron reset the environment variables, so don't load environment variables of the given users in the job definition lines.After creating
/etc/cron.d/myjob
35 * * * * t echo $PATH > /tmp/cron.log 2>&1
/tmp/cron.log
shows the default value of PATH is:/usr/bin:/bin
which isn't the root's PATH:
$ sudo su # echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
In
/etc/crontab
, I added* * * * * root echo $PATH > /tmp/cron.log 2>&1 * * * * * t echo $PATH > /tmp/cron.log.1 2>&1
Then The PATH value for a root's cron job isn't the root's
$ cat /tmp/cron.log /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
The PATH value of my cron job isn't mine (modified in
~/.profile
) either$ cat /tmp/cron.log.1 /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
$ echo $PATH /home/t/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/mssql-tools/bin
$ less ~/.profile | grep PATH PATH="$HOME/bin:$PATH" PATH="$PATH:/opt/mssql-tools/bin"
Thanks.
env >/tmp/env
and compare the various results from starting it in different cron files. – Chris Davies Nov 03 '18 at 09:35