如何定义crontab的run和bash shell可以访问的envrionmentvariables

我想在一个可以通过不同用户的crontabs访问的地方定义环境variables,并且从bash shell运行。

我怎样才能在一个地方写一次,以便上面提到的所有用法都可以访问?

到目前为止,我正在写入/ etc / bashrc中的系统范围的bashrc,而在crontabs中,我再次在crontab定义之上再次写入

我不认为你可以直接在crontab中做到这一点。 crontab文件不是shell脚本,所以试图将包含envvars的脚本发送到它不起作用

. /path/to/envvars 

crontab将不会安装,因为它无效

 crontab: installing new crontab "/tmp/crontab.iZqWJX/crontab":1: bad minute errors in crontab file, can't install. 

你可以尝试的就是在你运行我们的工作之前马上find一个包含你的环境的脚本

 * * * * * . /path/to/envars ; /path/to/your/job 

我testing了这个

 * * * * * . /home/iain/envvars ; echo $HELLO >/tmp/test.out 

包含envvars文件

 HELLO=fred 

文件/tmp/test.out包含以下内容

 fred 

所以我想这个工作。 source命令(。的同义词)是bash内build的,但是源码不是posix所以使用的. 是优选的。

 help source source: source filename [arguments] Execute commands from a file in the current shell. Read and execute commands from FILENAME in the current shell. The entries in $PATH are used to find the directory containing FILENAME. If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed. Exit Status: Returns the status of the last command executed in FILENAME; fails if FILENAME cannot be read