启动-停止-重新启动外壳脚本的功能



我是一个试图理解一些代码的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

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("用户")的进程的所有PID。-wgrep 的选项表示"仅整个单词"(这不包括查找的名称是另一个进程名称的一部分的情况,例如"fakememcached")。awk是一个小型解释器,最常用于从每行输入中获取单词数字 N(您可以将其视为文本表列的选择器)。在这种情况下,它会在输出行中打印ps每个单词,这意味着每个 PID。

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

以下是您不理解的代码段的说明:

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.
...

阿拉伯数字。

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

最新更新