我发现设置我的 crontabPATH=/home/toothrot/bin
有效,但
$HOME/bin
没有。我检查了一下,$HOME
设置是否符合预期。 cron 不扩展环境变量吗?我尝试通过调用路径中的脚本来使用它。
* * * * * fetchmail
PATH
当设置为完整显式路径时,此脚本会写入日志文件,但在使用 时不会写入日志文件$HOME
,可能是因为在后一种情况下它不会运行。
答案1
您没有指定您正在使用的 cron 实现,但至少在基于 Debian 的系统(它源自 Vixie cron IIRC)中没有指定。从man 5 crontab
:
An active line in a crontab will be either an environment setting or a
cron command. 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 subse‐
quent 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 leading or trailing blanks. To define an empty
variable, quotes must be used. The value string is not parsed for envi‐
ronmental substitutions or replacement of variables, 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