start-stop-restart shell脚本的功能

时间:2012-11-09 15:19:43

标签: linux bash shell unix console

我是一个shell脚本新手试图理解一些代码,但有一些行对我来说太复杂了。我正在谈论的那段代码可以在这里找到:https://gist.github.com/447191 它的目的是启动,停止和重新启动服务器。这是非常标准的东西,所以值得花一些时间来理解它。我评论了那些我不确定其含义或我完全不理解的那些内容,希望somone可以给我一些解释。

    #!/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

2 个答案:

答案 0 :(得分:2)

1) Unix传统中的COMMAND="$CMD -p $PORT -l $LISTEN_IP -m $MEM_SIZE -v" - -v通常是--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的LIST。首先,它尝试发送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')和user $ USR('user')的进程的所有PID。 grep的-w选项意味着'仅限整个单词'(这排除了所寻求的名称是另一个进程名称的一部分的情况,如'fakememcached')。 awk是一个小翻译,最常用于从每行输入中取一个单词数字N(您可以将其视为文本表的一列的选择器)。在这种情况下,它会在ps输出行中打印每个第二个单词,这意味着每个PID。

如果您有任何其他问题,我会在下面添加答案。

答案 1 :(得分: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相关的所有进程(实际上恰恰相反)。 以上是解释。