简体   繁体   English

Cygwin cron 读取 bash_profile 但不读取 bashrc

[英]Cygwin cron reads bash_profile but not bashrc

I am trying to a run a cronjob on my Windows machine with Cygwin that needs access to environment variables that I have defined in my ~/.bashrc.我正在尝试使用 Cygwin 在我的 Windows 机器上运行一个 cronjob,它需要访问我在 ~/.bashrc 中定义的环境变量。 Here all the different things I have tried to get it to access the env variable:这是我试图让它访问 env 变量的所有不同的东西:

  1. Simple sourcing of bashrc bashrc 的简单来源
PATH=/bin:/usr/bin:/usr/local/bin:$PATH
SHELL=/usr/bin/bash
*/1 * * * * source ~/.bashrc ; env | grep "MAP60" 
  1. Invoking bash and running the command调用 bash 并运行命令
PATH=/bin:/usr/bin:/usr/local/bin:$PATH
SHELL=/usr/bin/bash
*/1 * * * * /usr/bin/bash -c "source ~/.bashrc ; env | grep MAP60"
  1. Trying a bash login shell尝试使用 bash 登录 shell
PATH=/bin:/usr/bin:/usr/local/bin:$PATH
SHELL=/usr/bin/bash
 */1 * * * * /usr/bin/bash -lc " source ~/.bashrc ; env | grep MAP60"

None of these work, but case 3 works if I add the variable to my ~/.bash_profile.这些都不起作用,但如果我将变量添加到我的 ~/.bash_profile 中,则案例 3 有效。 I have also tried sourcing the bashrc with the full path, and also using .我还尝试使用完整路径获取 bashrc,并使用 . instead of source to invoke the file.而不是 source 来调用文件。 I've also tried just sourcing the bash_profile directly like in case 1, but that doesn't work either.我也试过像案例 1 一样直接获取 bash_profile ,但这也不起作用。 I would like to have the variables be called in the ~/.bashrc rather than having to call a login shell each time.我希望在 ~/.bashrc 中调用变量,而不必每次都调用登录 shell。 How can I achieve that with cygwin cron?如何使用 cygwin cron 实现这一目标?

Works for me (and everyone else).适用于我(和其他所有人)。 So something is wrong with your setup.所以你的设置有问题。 I don't have cron setup on Cygwin but one, very likely, possibility is that it can't email you the output of the command (something that is usually the default on a real UNIX).我没有在 Cygwin 上设置 cron,但很可能的一种可能性是它无法通过电子邮件将命令的输出发送给您(这通常是真正的 UNIX 上的默认设置)。 Start by modifying your third example thusly:首先修改你的第三个例子:

*/1 * * * * /usr/bin/bash -xlc " source ~/.bashrc ; env | grep MAP60" >/tmp/x 2>&1

Then check the contents of /tmp/x .然后检查/tmp/x的内容。

Note: Bash only reads ~/.bashrc for interactive shells.注意:对于交互式 shell,Bash 仅读取~/.bashrc Using -l does not make the shell interactive.使用-l不会使 shell 交互。 Putting var definitions in ~/.bashrc is not recommended.不建议将 var 定义放在~/.bashrc中。 But if you want to do this you should ensure any command that should only run if the shell is interactive is inside a if [[ $- = *i* ]]; then ...; fi但是如果你想这样做,你应该确保任何只在 shell 交互时才运行的命令都在if [[ $- = *i* ]]; then ...; fi if [[ $- = *i* ]]; then ...; fi if [[ $- = *i* ]]; then ...; fi block in your ~/.bashrc . if [[ $- = *i* ]]; then ...; fi块在你的~/.bashrc 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM