【问题标题】:Functionality of a start-stop-restart shell script启动-停止-重启 shell 脚本的功能
【发布时间】:2012-11-09 15:19:43
【问题描述】:

我是一个 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

【问题讨论】:

  • 啊,ps | grep -v grep。这是一个常见的成语。第一个ps ...grep(显示单个pid 的“完整”输出)更有效地编写ps -f $(cat $PID)ps ... awk(显示某个用户的 pid)更有效地写成ps -u $USR -o pid=

标签: linux bash shell unix console


【解决方案1】:

1) COMMAND="$CMD -p $PORT -l $LISTEN_IP -m $MEM_SIZE -v" — 在 Unix 传统中,-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 列表。首先,它尝试发送 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。 grep 的-w 选项表示“仅整个单词”(这不包括所寻求的名称是另一个进程名称的一部分的情况,例如“fakememcached”)。 awk 是一个小型解释器,最常用于从输入的每一行中获取一个单词编号 N(您可以将其视为文本表列的选择器)。在这种情况下,它会在 ps 输出行中每隔一个单词打印一次,这意味着每个 PID。

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

【讨论】:

    【解决方案2】:

    下面是对你不明白的代码片段的解释:

    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相关的进程(实际上恰恰相反)。 以上是解释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多