bash脚本 - 检查多个工作进程

时间:2015-08-02 00:56:05

标签: bash shell

我想使用此bash脚本检查多个进程(具有相似名称)是否正在运行。进程名称跟随一个特定的约定worker1.gs,其中X是一个序列号,其值为1到4.我有一个片段用于检查worker 1和2等等......但是我想将值保存在array [1中, 2,3.4]并循环检查它..怎么做?在检查第一个进程后,脚本终止也会终止。还要修复它吗?

  #!/bin/bash

  pidof /usr/bin/gsx /var/app/transcoding/worker1.gs >/dev/null
  if [[ $? -ne 0 ]] ; then
    echo "Restarting Worker1: $(date)" >> /var/log/httpd/watchdog.txt
    /usr/bin/gsx /var/app/transcoding/worker1.gs
  fi

  pidof /usr/bin/gsx /var/app/transcoding/worker2.gs >/dev/null
  if [[ $? -ne 0 ]] ; then
    echo "Restarting Worker2: $(date)" >> /var/log/httpd/watchdog.txt
    /usr/bin/gsx /var/app/transcoding/worker2.gs
  fi

1 个答案:

答案 0 :(得分:1)

你可以在C:中创建循环:

for((i=0;i<10;i++)); do echo "$i" ; done

你可以精简

pidof /usr/bin/gsx /var/app/transcoding/worker1.gs >/dev/null
if [[ $? -ne 0 ]] ; then ... fi

进入

pidof /usr/bin/gsx /var/app/transcoding/worker1.gs >/dev/null || { ... }

组合:

for((i=0;i<10;i++)); do
  pidof /usr/bin/gsx /var/app/transcoding/worker1.gs >/dev/null || {
     echo "Restarting Worker$i: $(date)" >> /var/log/httpd/watchdog.txt
     /usr/bin/gsx /var/app/transcoding/worker$i.gs
  }   
done