【问题标题】:How to use environment variable declared in /etc/profie into script running through monit?如何将 /etc/profile 中声明的环境变量用于通过监视器运行的脚本?
【发布时间】:2014-02-21 03:01:31
【问题描述】:

/etc/profile 中声明的环境变量:

export MYNAME=rhel

从 monit 运行的脚本内容是 [/tmp/printmyname.sh]:

echo "My Name is: "$MYNAME >> /var/log/env_variablefile.out

监控内容:

check file PrintVariable with path /var/log/something.out
   start program = "/bin/sh /tmp/printmyname.sh"
   if timestamp > 1 minutes then start

/var/log/something.out 文件一分钟后没有更新时,我想打印在/etc/profile 中声明的环境变量到/var/log/env_variablefile.out

所以我的问题是,当我直接运行 /tmp/printmyname.sh 时,它会将 My Name is: rhel 附加到 /var/log/env_variablefile.out 中,但是当它从 monit 运行时,它只会打印 My Name is:

所以我想知道这种行为的原因和解决问题的方法。

注意:monit 每 10 秒运行一次,以上代码只是我实际代码的示例。

【问题讨论】:

    标签: shell rhel monit


    【解决方案1】:

    /etc/profile 仅对交互式 shell 执行。

    解决此问题的一种方法是将其添加到 /tmp/printmyname.sh 的开头:

    . /etc/profile
    

    请注意,这可能会导致问题,因为/etc/profile 尝试设置交互式环境,因此会调用很多您可能不想要的设置脚本。

    更好的解决方案可能是将这个变量放在一个新的全局脚本中,并从/etc/profile/tmp/printmyname.sh 获取这个新脚本

    相关文章:

    【讨论】:

      猜你喜欢
      • 2014-04-25
      • 2017-06-24
      • 2022-12-24
      • 2017-12-27
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多