Como o cron define as variáveis de ambiente em /etc/cron.d/* e /etc/cron.d/?

0

Na página do Debian do cron,

The files in /etc/cron.d/ are independent of /etc/crontab: they do not, for example, inherit environment variable settings from it.

e no Fórum LinuxQuestions.org: / 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.

Eu queria saber o que as frases destacadas por mim significam? De que é o "herdar" o ance de?

  1. Para /etc/cron.d/* , cron resetar as variáveis de ambiente, então não Carregar Variáveis de Ambiente dos Usuários Especificados na Definição do Job linhas.

    Depois de criar /etc/cron.d/myjob

    35 * * * * t   echo $PATH  > /tmp/cron.log 2>&1
    

    /tmp/cron.log mostra o valor padrão de PATH:

    /usr/bin:/bin
    

    que não é o caminho da raiz:

    $ sudo su
    # echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
    
  2. Em /etc/crontab , adicionei

    * * * * * root   echo $PATH    > /tmp/cron.log 2>&1
    * * * * * t    echo $PATH  > /tmp/cron.log.1 2>&1
    

    Em seguida, o valor PATH para a tarefa cron de uma raiz não é da raiz

    $ cat /tmp/cron.log
    /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    

    O valor PATH do meu trabalho cron não é meu (modificado em ~/.profile ) ou

    $ 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"
    

Obrigado.

    
por Tim 03.11.2018 / 04:25

1 resposta

1

De man 5 crontab :

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

Ou seja: uma linha não comentada ( # ) poderia ser:

PATH = /bin:/sbin

Que definirá o valor de PATH para todo o arquivo crontab .

Se esse valor não for definido, o valor interno (em código) conforme mostrado nesta resposta é usado.

Exemplo de um arquivo crontab definindo o 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"
    
por 05.11.2018 / 06:30

Tags