4

我是一个 shell 脚本新手,试图理解一些代码,但是有些行对我来说太复杂了。我正在谈论的这段代码可以在这里找到: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"-v在 Unix 传统中,很多时候是--verbose. 所有这些美元符号都是变量扩展(它们的文本值被插入到分配给新变量 COMMAND 的字符串中)。

2) ps -ef | grep -v grep | grep $( cat $PID )- 它是一个管道: ps 将其输出重定向到 grep 输出到另一个 grep 并且最终结果被打印到标准输出。 grep -v grep意思是“取所有包含 'grep' 的行”(grep 本身就是一个过程,所以你需要从 的输出中排除它ps)。$( $command )是一种运行命令并将其标准输出插入此脚本位置的方法(在这种情况下:cat $PID将显示名称为 $PID 的文件的内容)。

3) kill_cmd。这个函数是一个无限循环,试图杀死“memcached”进程的 PID 列表。首先,它尝试发送 TERM 信号(礼貌地要求 $LIST 中的每个进程退出,保存其工作并正确关闭),给它们 2 秒(sleep 2)来完成它们的关闭工作,然后尝试确保所有进程都被杀死使用信号 KILL ( -9),它使用操作系统工具立即杀死进程:如果进程在 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,这意味着每个 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 grep) 的行(因为您正在运行 grep,它将在进程列表中显示自己)并通过名为 $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 回答