【问题标题】:How to grep the output of a command inside a shell script when scheduling using cron使用cron进行调度时如何在shell脚本中grep命令的输出
【发布时间】:2021-12-29 04:46:31
【问题描述】:

我有一个简单的 shell 脚本,我需要检查我的 EMR 作业是否正在运行,我只是在打印一个日志,但是在使用 cron 调度脚本时它似乎无法正常工作,因为它总是打印 if 块声明,因为“status_live” var 的值始终为空,所以如果有人可以在这里提出问题,否则在手动运行脚本时它可以正常工作。

#!/bin/sh

status_live=$(yarn application -list | grep -i "Streaming App")

if [ -z $status_live ] 
then
  echo "Running spark streaming job again at: "$(date) &
else
  echo "Spark Streaming job is running, at: "$(date)
fi

【问题讨论】:

    标签: shell cron grep amazon-emr script


    【解决方案1】:

    您的脚本无法在 cron 中运行,因为 cron 脚本根本没有环境上下文。

    例如,尝试将您的脚本作为另一个没有外壳的 nobody 运行。

     sudo -u nobody <script-full-path>
    

    它将失败,因为它没有环境上下文。

    解决方案是将您的用户环境上下文添加到您的脚本中。只需将source 添加到您的.bash_profile

     sed -i "2a source $HOME/.bash_profile" <script-full-path>
    

    您的脚本应如下所示:

    #!/bin/sh
    source /home/<your user name>/.bash_profile
    
    status_live=$(yarn application -list | grep -i "Streaming App")
    
    if [ -z $status_live ] 
    then
      echo "Running spark streaming job again at: "$(date) &
    else
      echo "Spark Streaming job is running, at: "$(date)
    fi
    

    现在尝试使用用户nobody 再次运行它,如果它可以正常工作,那么cron 也可以正常工作。

     sudo -u nobody <script-full-path>
    

    请注意,cron 没有标准输出。并且您需要将脚本中的标准输出重定向到日志文件。

    <script-full-path> >> <logfile-full-path>
    

    【讨论】:

      【解决方案2】:
      # $? will have the last command status in bash shell scripting
      # your complete command here below and status_live is 0  if it finds in grep (i.e. true in shell if condition.)
      yarn application -list | grep -i "Streaming App"
      status_live=$?
      echo status_live: ${status_live}
      if [ "$status_live" -eq 0 ]; then
          echo "success
      else
          echo "fail" 
      fi
      

      【讨论】:

      • 实际上的问题是,当我手动运行脚本时,上面的 shell 脚本也运行良好,但是在使用 cron 调度它时,它不能按预期工作,并且这个脚本也会发生同样的情况好吧。
      • @YashTandon 看起来像在 cron 中,它可能在不同的 shell 环境下运行。
      • @YashTandon SHELL=/bin/bash 在你的命令运行之前在 cron 文件中,只是为了确保它在 bash 下运行
      • @tripleee $?将始终在 shell 中给出先前命令的成功或失败
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2017-02-21
      • 2020-06-04
      相关资源
      最近更新 更多