源bashrc在cron中不起作用 [英] source bashrc doesn't work in cron
问题描述
我们都知道cron会忽略".bashrc"和".bash_profile"中定义的变量,因此我们必须在cron内对其进行定义.我经常做同样的事情,写在类似的问题https://unix.stackexchange.com/questions/67940/cron-ignores-variables-defined-in-bashrc-and-bash-profile ,但.bashrc内的全局变量仍然无法正常工作.我找到了执行它的方法-通过使用"set + a" bashrc脚本定义sh脚本.但是源"仍然不起作用.
All we know that cron ignores variables defined in ".bashrc" and ".bash_profile", so we have to define it inside cron. I constantly do the same what was written inside the similar question https://unix.stackexchange.com/questions/67940/cron-ignores-variables-defined-in-bashrc-and-bash-profile but still global variables inside .bashrc still not working. I found way to execute it - by defining sh script with "set +a" bashrc script. But "source" still doesn't work.
SHELL=/bin/bash
BASH_ENV=/root/.bashrc
PATH=:/opt/spark/spark-2.2.0-bin-hadoop2.7/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
SPARK_HOME=/opt/spark/spark-2.2.0-bin-hadoop2.7
MAILTO=root HOME=/
# m h dom mon dow command
* * * * * /bin/bash -c 'source $HOME/.bashrc; echo "SPARK_HOME: '$SPARK_HOME'"; echo "JAVA_HOME: '$JAVA_HOME'"' > /var/log/file.log 2>&1
# DO NOT DELETE LAST LINE
返回日志文件
SPARK_HOME: /opt/spark/spark-2.2.0-bin-hadoop2.7
JAVA_HOME:
还尝试以交互方式执行此操作,因为它是由mklement0编写的.
源.bashrc在脚本中不起作用
also tried to execute this in interactive mode as it was written by mklement0
source .bashrc in a script not working
* * * * * /bin/bash -i source /root/.bashrc; echo $JAVA_HOME > /var/log/file.log 2>&1
如您所见,SPARK_HOME在crontab中定义,而JAVA_HOME仅在.bashrc中定义.bashrc java home中的P.S定义为"export JAVA_HOME =/usr/jdk1.8.0_131"当我将JAVA_HOME更改为SPARK_HOME时,所有不同的cron作业均能正常工作,尝试了不同的crontab作业,但答案与以前相同.
As you can see SPARK_HOME is defined inside crontab whilst JAVA_HOME only in .bashrc. P.S in bashrc java home is defined "export JAVA_HOME=/usr/jdk1.8.0_131" All different cron jobs work fine when I change JAVA_HOME to SPARK_HOME, tried different crontab jobs but the answer is the same as it was.
ubuntu内核版本为
ubuntu kernel version is
uname -a
Linux 6101c32b9243 4.9.62-21.56.amzn1.x86_64 #1 SMP Thu Nov 16 05:37:08 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
推荐答案
您最后一次尝试运行 bash
并获取文件,然后退出Bash并在调用shell,它不是Bash,当然也不知道或不在乎现在已经消失的Bash进程加载了一些设置,然后在退出时忘记了它们.(或者,如果您修复了简单的引用错误以将 source
命令及其参数保留为单个字符串,将会发生这种情况.)
Your last attempt runs bash
and sources the file, then exits Bash and runs the echo
in the calling shell, which isn't Bash, and of course doesn't know or care that the now-defunct Bash process loaded some settings and then forgot them when it exited. (Or, well, this is what would happen if you fixed the simple quoting errors to keep the source
command and its argument as a single string.)
表面修复很容易;
* * * * * bash -c 'source $HOME/.bashrc; echo "$JAVA_HOME"' >/var/log/file.log 2>&1
适当修复程序可能是将所有这些代码封装在一个单独的脚本中,并使您的 crontab
非常简单.
The proper fix, however, is probably to encapsulate all of this in a separate script and keep your crontab
really simple.
(您确实确定允许用户覆盖日志文件吗?每分钟替换一次日志似乎很误导,尽管足以进行快速测试以查看该作业是否正在运行.)
(Are you really sure your user is allowed to overwrite the log file? Replacing the log once a minute seems rather misdirected, although it's good enough for a quick test to see if the job is running at all.)
这篇关于源bashrc在cron中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!