一個短小精悍的監控/守護程式

鍾超發表於2012-05-28

一個短小精悍的監控/守護程式

  • Author: 柳大·Poechant(鍾超)
  • Email: zhongchao.ustc#gmail.com
  • Blog:Blog.CSDN.net/Poechant
  • Date: May 28th, 2012

#!/bin/bash

YOUR_TARGET = your_target

while:
do
    echo "Current DIR is " $PWD
    stillRunning = $(ps -ef | grep "$PWD/$YOUR_TARGET" | grep -v "grep")
    if [ "$stillRunning" ]; then
        echo "You target service was already started by another way"
        echo "Kill it and then startup by this shell, otherwise this shell will loop out this message annoyingly"
        kill -9 $pidof $PWD/$YOUR_TARGET
    else
        echo "Your target service was not started"
        echo "Starting service …"
        $PWD/$YOUR_TARGET &
        echo "Your target service was exited!"
    fi
    sleep 10
done
  • grep -v: –invert-match. Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)

-

轉載請註明來自柳大的CSDN部落格:Blog.CSDN.net/Poechant

-

相關文章