0

I'm try to make some shell script work like below.

 #!/bin/bash
 while [ 1 ]
    do
    pid=`ps -ef | grep "sample.exe" | grep -v 'grep' | awk '{print $2}'`
         if [ -z $pid ]; then
            nohup ./run.sh > /dev/null &  
            #./run.sh is shell script for run 'sample.exe' file
         fi
    usage=`ps -ef | grep "sample_watcher.sh" | grep -v 'grep' | awk '{print $2}'`
         if [ -z $usage ]; then
            $kill $(ps aux | grep "sample_watcher.sh"| awk '{print $2}')
         fi
        sleep 5
    done



"sample_watcher.sh" 15L, 491C

this sh = "sample_watcher.sh". and this will work find sample.exe is runnig or not.

and if not running, then start run.sh = start up 'sample.exe'

and last part

if "sample_watcher.sh"is already run. then kill "sample_watcher.sh".

only one "sample_watcher.sh" should be run.

Is there better way to do?

Thanks.

hybang
  • 65
  • 10
  • Search for "bash lock" or "bash execution lock", there [are](http://stackoverflow.com/q/25863930/113632) [several](http://stackoverflow.com/q/29185116/113632) [similar](http://stackoverflow.com/q/39666334/113632) [questions](http://stackoverflow.com/q/22506857/113632) you might find helpful. – dimo414 Apr 13 '17 at 04:37
  • Besides `flock` there's also [`watershed`](https://launchpad.net/watershed). – agc Apr 13 '17 at 04:53

0 Answers0