2

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?

  1. 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
    
  2. 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.

2
  • You can find out easily enough. Create a job that does env >/tmp/env and compare the various results from starting it in different cron files. Commented Nov 3, 2018 at 9:35
  • Thanks for the suggestion. Please see my update.
    – Tim
    Commented Nov 3, 2018 at 15:14

1 Answer 1

4

From man 5 crontab:

An active line in a crontab will be either an environment setting or a cron command.

That is: a not commented line (#) could be:

PATH = /bin:/sbin

Which will set the value of PATH for the whole crontab file.

If that value is not set, the built-in value (in-code) as shown on this answer is used.

Example of a crontab file setting the PATH:

SHELL=/bin/bash 
MAILTO=root
PATH=~/bin:/usr/bin/:/bin

# Edit this file to introduce tasks to be run by cron.
#.---------------- minute (m) (0 - 59)
#|      .------------- hour (h) (0 - 23)
#|      |       .---------- day of month (dom) (1 - 31)
#|      |       |       .------- month (mon) (1 - 12) OR jan,feb,mar,apr ...
#|      |       |       |       .---- day of week (dow) (0 - 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
#                                       .---- user
#|      |       |       |       |       |
#*      *       *       *       *       root   echo "the command to be executed"
#
#m      h       dom     mon     dow     user   command
*       *       *       *       *       root   echo "A crontab file test"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .