如何在 Centos 6.4 上为 cron 设置系统范围的 PATH 变量

如何在 Centos 6.4 上为 cron 设置系统范围的 PATH 变量

我想确保每个用户特定的 crontab 都具有相同的 PATH 环境。我尝试在 /etc/crontab 中设置 PATH,但似乎 crond 无法读取此配置文件。

以下是我的机器上安装的 cron 相关软件包:

root@machine:~> rpm -qa | grep cron
cronolog-1.6.2-10.el6.x86_64
cronie-anacron-1.4.4-7.el6.x86_64
cronie-noanacron-1.4.4-7.el6.x86_64
crontabs-1.10-33.el6.noarch
cronie-1.4.4-7.el6.x86_64

更新:我的 /etc/crontab

这么晚才回复很抱歉。

SHELL=/bin/bash
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

答案1

你确定你设置$PATH正确了吗?看一下man 5 crontab

An  active  line in a crontab will be either an environment setting or a cron com‐
mand.  The crontab file is parsed from top to bottom, so any environment  settings
will affect only the cron commands below them in the file.  An environment setting
is of the form,

    name = value

where the spaces around the equal-sign (=) are optional, and any  subsequent  non-
leading  spaces  in  value  will be part of the value assigned to name.  The value
string may be placed in quotes (single or double, but matching) to preserve  lead‐
ing  or  trailing  blanks.  To  define an empty variable, quotes must be used. The
value string is not parsed for environmental substitutions or replacement of vari‐
ables, thus lines like

    PATH = $HOME/bin:$PATH

will not work as you might expect. And neither will this work

    A=1
    B=2
    C=$A $B
There will not be any subsitution for the defined variables in the last value.

An alternative for setting up the commands path is using the fact that many shells
will treat the tilde(~) as substitution of $HOME, so if  you  use  bash  for  your
tasks you can use this:

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

相关内容