0

I jave written a small bash script to run every 10 minutes, check if a certain program is running and if not run it. However, I noticed that sometimes I have two instances of the same program running (using ps ax | grep "gateway"). What am I doing wrong?

#! /bin/bash                                                                   
case "$(ps ax | grep -c gateway.jar)" in                                       

      1)  echo "Restarting Java Gateway (1):     $(date)" >> /home/user/gateway.log
          java -jar /home/user/project/unx/java/gateway.jar &          
      ;;                                                                         
      0)  echo "Restarting Java Gateway (0):     $(date)" >> /home/iwidgetuser/gateway.log
          java -jar /home/user/project/unx/java/gateway.jar &          
      ;;                                                                         
      *)  echo "Gateway is running! Good!:     $(date)" >> /home/user/gateway.log
      ;;                                                                         
esac                                                                           
2
  • 1
    On Linux flock is your friend.
    – user9517
    Oct 27 '14 at 19:58
  • @Iain, thanks. What do you mean by "/path/to/your/job"? Is it a bash script?
    – xpanta
    Oct 27 '14 at 20:36
1

Your check is program is running is incorrect.

You obtain list of running processes and search in them string with your program name (gateway.jar). If string occured in processes list 0 or 1 times you start gateway.jar.

If gateway.jar is still running it will be in process list. Also, if grep invocation was caught in process list, it's command will contain gateway.jar and it will be counted by grep.

So there are three four cases:

  1. gateway.jar is running and grep is in process list -> grep returns 2

  2. gateway.jar is running and grep is not in process list -> grep returns 1

  3. gateway.jar is not running and grep is in process list -> grep returns 1

  4. gateway.jar is not running and grep is not in process list -> grep returns 0

If case 2 happens your code starts gateway.jar second time.

Case 2 happens rarely, e.g. on my machine it happened 4 times out of 1000 invocations:

$ for i in `seq 1 1000`; do if [ $(ps ax | grep -c abcd) != "1" ]; then echo $i; fi; done
183
377
632
898
$
2
  • Thank you. This clarifies a lot. I am trying to use flock. as Iain suggested, but until then. Is there a way to fix my code?
    – xpanta
    Oct 27 '14 at 21:03
  • 1
    As a quick fix you can search with regexp pattern that will not match itself, e.g. $(ps ax | grep -c -E 'gateway\.jar') will be either 0 or 1. -E enables handling pattern as regexp, and \. is escaping of the dot character.
    – rutsky
    Oct 27 '14 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.