【问题标题】:Callback based on exit status of command in bash shell scripting? [duplicate]基于bash shell脚本中命令退出状态的回调? [复制]
【发布时间】:2017-10-21 13:33:23
【问题描述】:

我有 10 个并行执行的命令:

comdA & comdB & comdC & comdD

如果这些命令中的任何一个返回的退出状态不是 0,有没有办法执行回调?

如果 bash 无法做到这一点。 php怎么样?可以吗

function exec_with_callback ($comd) {

    shell_exec($comd);
    callback();

}

exec_with_callback("comdA");
exec_with_callback("comdB");
...
but in parallel?

如果不是,我可以使用其他什么语言?

【问题讨论】:

标签: php bash shell


【解决方案1】:

您可以循环执行命令并使用$! shell 变量保存 process_ids,该变量为您提供最后一个后台作业的进程 ID。

n=0
commands=(comdA  comdB comdC comdD)  #storing all 10 commands  in an array. store the status of each execution in another array
for cmd in ${commands[@]}; do
  ${cmd} &
  pid=$!
  pidarray[$n]=${pid}
  ((n+=1))
done

在循环中使用wait <PID> 等待所有进程完成。

n=0
for pid in ${pidarray[@]}; do
  wait ${pid}
  exit_status_array[$n]=$?
  ((n+=1))
done

现在循环exit_status_array,如果退出状态不是0,则回调相应的命令

n=0
for s in ${exit_status_array[@]}; do
  if [[ ${s} -ne 0 ]]; then
   commands[$n] &    #callback
  fi
  ((n+=1))
done

如果您愿意,可以通过使用此逻辑并调用函数等无限期地重复该过程。

【讨论】:

    【解决方案2】:

    也许你可以用这个:

    #!/bin/bash
    
    function check () {
        $1 >/dev/null 2>&1
        echo $?
    }
    
    command=("curl -sSL google.com" "echo 1" 'ping localhost -c 1' 'ls' 'false')
    
    for ((i=0;i<${#command[@]};i++)); do
        echo "Command \"${command[$i]}\" returned value $(check "${command[$i]}")"
        if (($(check "${command[$i]}") != 0)); then second=1; fi
    done
    
    if ((second == 1)); then
        echo "I must run second group of commands because something have not worked!"
        echo 2
    else
        echo "All is gone without issues! Goodbye $USER!"
        exit 0
    fi
    

    检查函数运行命令并返回退出状态,命令进入命令数组,通过for循环我们可以看到所有运行的命令及其退出状态。如果某人的退出状态不等于零,则当所有命令都与其他命令一起完成时,一个变量可以帮助我们运行 if。

    输出示例

    darby@Debian:~/Scrivania$ bash example
    Command "curl -sSL google.com" returned value 0
    Command "echo 1" returned value 0
    Command "ping localhost -c 1" returned value 0
    Command "ls" returned value 0
    Command "false" returned value 1
    I must run second group of commands because something have not worked!
    2
    darby@Debian:~/Scrivania$ bash example
    Command "curl -sSL google.com" returned value 0
    Command "echo 1" returned value 0
    Command "ping localhost -c 1" returned value 0
    Command "ls" returned value 0
    Command "true" returned value 0
    All is gone without issues! Goodbye darby!
    darby@Debian:~/Scrivania$
    

    【讨论】:

      【解决方案3】:

      准备一个包含一行命令的文件myCommands

      echo "curl -sSL google.com" > myCommands.txt
      echo "echo 1"               >> myCommands.txt
      echo "ping localhost -c 1"  >> myCommands.txt
      echo "ls"                   >> myCommands.txt
      echo "false"                >> myCommands.txt
      

      然后使用xargs,检查xargs的返回码。

      xargs --arg-file myCommands.txt --max-procs 5 -I COMMAND sh -c "COMMAND"
      [ $? -eq 0 ] && echo "All command have sucessfully finish with retrun code 0" && echo "You need check the result" 
      

      如果代码返回等于 0 ==> myCommands 文件中的所有命令都已成功完成,返回代码为 0

      --max-procs :一次运行最多 max-procs 个进程;默认值为 1。如果 max-procs 为 0,xargs 将运行尽可能多的进程 一次。

      【讨论】:

        猜你喜欢
        • 2013-03-01
        • 2014-12-03
        • 2017-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-03
        • 1970-01-01
        相关资源
        最近更新 更多