JVK
JVK

Reputation: 3912

bash script - checking multiple worker processes

I want to check multiple processes (have similar name) running or not, using this bash script. Processes names follwo a particular convention worker1.gs where X is a serial number have value 1 to 4. I have a snippet there for checking worker 1 and 2 and so on... but I want to save the value in array [1,2,3.4] and loop through it to check it ..how to do that? Also below script terminate after checking first process..how to fix that as well?

  #!/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

Upvotes: 0

Views: 88

Answers (1)

Petr Skocik
Petr Skocik

Reputation: 60058

You can make loops like in C:

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

and you can streamline

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

into

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

Combined:

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

Upvotes: 1

Related Questions