4

私はいくつかのコードを理解しようとしているシェルスクリプトの初心者ですが、複雑すぎる行がいくつかあります。私が話しているコードは、https ://gist.github.com/447191 にあります。目的は、サーバーを起動、停止、再起動することです。これはかなり標準的なものなので、理解するのに少し時間がかかります。意味がわからない、または完全に理解できない行にコメントしました。誰かが説明してくれることを願っています。

    #!/bin/bash
    #
    BASE=/tmp
    PID=$BASE/app.pid
    LOG=$BASE/app.log
    ERROR=$BASE/app-error.log

    PORT=11211
    LISTEN_IP='0.0.0.0'
    MEM_SIZE=4
    CMD='memcached'
# Does this mean, that the COMMAND variable can adopt different values, depending on
# what is entered as parameter? "memcached" is chosen by default, port, ip address and 
# memory size are options, but what is -v?
    COMMAND="$CMD -p $PORT -l $LISTEN_IP -m $MEM_SIZE -v"

    USR=user

    status() {
        echo
    echo "==== Status"

        if [ -f $PID ]
        then
    echo
    echo "Pid file: $( cat $PID ) [$PID]"
            echo
# ps -ef: Display uid, pid, parent pid, recent CPU usage, process start time, 
# controling tty, elapsed CPU usage, and the associated command of all other processes
# that are owned by other users.
# The rest of this line I don't understand, especially grep -v grep
    ps -ef | grep -v grep | grep $( cat $PID )
        else
    echo
    echo "No Pid file"
        fi
    }

    start() {
        if [ -f $PID ]
        then
    echo
    echo "Already started. PID: [$( cat $PID )]"
        else
    echo "==== Start"
# Lock file that indicates that no 2nd instance should be started
            touch $PID
# COMMAND is called as background process and ignores SIGHUP signal, writes it's
# output to the LOG file. 
            if nohup $COMMAND >>$LOG 2>&1 &
# The pid of the last background is saved in the PID file
            then echo $! >$PID
                 echo "Done."
                 echo "$(date '+%Y-%m-%d %X'): START" >>$LOG
            else echo "Error... "
                 /bin/rm $PID
            fi
    fi
    }
# I don't understand this function :-(    
    kill_cmd() {
        SIGNAL=""; MSG="Killing "
        while true
    do
    LIST=`ps -ef | grep -v grep | grep $CMD | grep -w $USR | awk '{print $2}'`
            if [ "$LIST" ]
            then
    echo; echo "$MSG $LIST" ; echo
    echo $LIST | xargs kill $SIGNAL
# Why this sleep command?
                sleep 2
                SIGNAL="-9" ; MSG="Killing $SIGNAL"
                if [ -f $PID ]
                then
                    /bin/rm $PID
                fi
    else
    echo; echo "All killed..." ; echo
    break
    fi
    done
    }

    stop() {
        echo "==== Stop"

        if [ -f $PID ]
        then
    if kill $( cat $PID )
            then echo "Done."
                 echo "$(date '+%Y-%m-%d %X'): STOP" >>$LOG
            fi
            /bin/rm $PID
            kill_cmd
        else
    echo "No pid file. Already stopped?"
        fi
    }

    case "$1" in
        'start')
                start
                ;;
        'stop')
                stop
                ;;
        'restart')
                stop ; echo "Sleeping..."; sleep 1 ;
                start
                ;;
        'status')
                status
                ;;
        *)
                echo
    echo "Usage: $0 { start | stop | restart | status }"
                echo
    exit 1
                ;;
    esac

    exit 0
4

2 に答える 2

2

1) COMMAND="$CMD -p $PORT -l $LISTEN_IP -m $MEM_SIZE -v"-vUnix の伝統では、 は のショートカットであることが非常に多いです--verbose。これらのドル記号はすべて変数展開です (それらのテキスト値は、新しい変数 COMMAND に割り当てられた文字列に挿入されます)。

2) ps -ef | grep -v grep | grep $( cat $PID )- パイプです: ps はその出力を grep にリダイレクトし、その出力は別の grep に出力され、最終結果が標準出力に出力されます。 「'grep' を含まないgrep -v grepすべての行を取得する」ことを意味します (grep 自体はプロセスなので、の出力から除外する必要があります)。コマンドを実行し、その標準出力をスクリプトのこの場所に挿入する方法です (この場合: $PID という名前のファイルの内容が表示されます)。ps$( $command )cat $PID

3) kill_cmd. この関数は、「memcached」プロセスの PID のリストを強制終了しようとする無限ループです。最初に、TERM シグナルの送信を試み ($LIST 内の各プロセスに丁寧に終了を要求し、その作業を保存して正しくシャットダウンします) sleep 2、シャットダウン ジョブを実行するために 2 秒 ( ) 与えてから、すべてのプロセスが強制終了されていることを確認しようとします。シグナル KILL ( -9) を使用して、OS 機能を使用してプロセスを即座に停止します: プロセスが 2 秒以内にシャットダウン作業を完了しなかった場合、そのプロセスはハングしたと見なされます)。での殺害kill -9が成功した場合、PID ファイルを削除し、ループを終了します。

ps -ef | grep -v grep | grep $CMD | grep -w $USR | awk '{print $2}'名前が $CMD ('memcached') でユーザーが $USR ('user') のプロセスのすべての PID を出力します。-wgrep のオプションは、「完全な単語のみ」を意味します (これは、検索された名前が「fakememcached」のような別のプロセス名の一部である状況を除外します)。awk入力のすべての行から単語番号 N を取得するために最も頻繁に使用される小さなインタープリターです (テキスト テーブルの列のセレクターと見なすことができます)。この場合、ps出力行に 1 つおきの単語、つまりすべての PID を出力します。

他に質問があれば、以下に回答を追加します。

于 2012-11-09T16:06:12.453 に答える
1

理解できないコードの説明は次のとおりです。

1.

# Does this mean, that the COMMAND variable can adopt different values, depending on
# what is entered as parameter? "memcached" is chosen by default, port, ip address and 
# memory size are options, but what is -v?
    COMMAND="$CMD -p $PORT -l $LISTEN_IP -m $MEM_SIZE -v"

男性の -v の近く:

$ man memcached
...
 -v     Be verbose during the event loop; print out errors and warnings.
...

2.

# ps -ef: Display uid, pid, parent pid, recent CPU usage, process start time, 
# controling tty, elapsed CPU usage, and the associated command of all other processes
# that are owned by other users.
# The rest of this line I don't understand, especially grep -v grep
ps -ef | grep -v grep | grep $( cat $PID )

すべてのプロセスの詳細を出力し ( ps -ef)、grep ( ) で行を除外し ( grep -v grepgrep を実行しているため、プロセス リストに表示されます)、$PID ( /tmp/app.pid) ( grep $( cat $PID )) という名前のファイルにあるテキストでフィルター処理します。

3.

# I don't understand this function :-(    
    kill_cmd() {
        SIGNAL=""; MSG="Killing "
        while true
    do
    ## create a list with all the pid numbers filtered by command (memcached) and user ($USR)
    LIST=`ps -ef | grep -v grep | grep $CMD | grep -w $USR | awk '{print $2}'`
    ## if $LIST is not empty... proceed
            if [ "$LIST" ]
            then
    echo; echo "$MSG $LIST" ; echo
    ## kill all the processes in the $LIST (xargs will get the list from the pipe and put it at the end of the kill command; something like this < kill $SIGNAL $LIST > )
    echo $LIST | xargs kill $SIGNAL
# Why this sleep command?
## some processes might take one or two seconds to perish
                sleep 2
                SIGNAL="-9" ; MSG="Killing $SIGNAL"
                ## if the file $PID still exists, delete it
                if [ -f $PID ]
                then
                    /bin/rm $PID
                fi
    ## if list is empty
    else
    echo; echo "All killed..." ; echo
    ## get out of the while loop
    break
    fi
    done
    }

この関数は、関連するすべてのプロセスをmemcachedゆっくりと苦痛を伴うように強制終了します (実際にはまったく逆です)。以上が解説です。

于 2012-11-09T16:24:18.610 に答える