Functionality of a start-stop-restart shell script Functionality of a start-stop-restart shell script unix unix

Functionality of a start-stop-restart shell script


1)COMMAND="$CMD -p $PORT -l $LISTEN_IP -m $MEM_SIZE -v"-v in Unix tradition very often is a shortcut for --verbose. All those dollar signs are variable expansion (their text values are inserted into the string assigned to new variable COMMAND).

2) ps -ef | grep -v grep | grep $( cat $PID ) - it's a pipe: ps redirects its output to grep which outputs to another grep and the end result is printed to the standard output.grep -v grep means "take all lines that do not contain 'grep'" (grep itself is a process, so you need to exclude it from output of ps). $( $command ) is a way to run command and insert its standard output into this place of script (in this case: cat $PID will show contents of file with name $PID).

3) kill_cmd.This function is an endless loop trying to kill the LIST of 'memcached' processes' PIDs. First, it tries to send TERM signal (politely asking each process in $LIST to quit, saving its work and shutting down correctly), gives them 2 seconds (sleep 2) to do their shutdown job and then tries to make sure that all processes are killed using signal KILL (-9), which slays the process immediately using OS facilities: if a process has not done its shutdown work in 2 seconds, it's considered hung). If slaying with kill -9 was successful, it removes the PID file and quits the loop.

ps -ef | grep -v grep | grep $CMD | grep -w $USR | awk '{print $2}' prints all PIDs of processes with name $CMD ('memcached') and user $USR ('user'). -w option of grep means 'the Whole word only' (this excludes situations where the sought name is a part of another process name, like 'fakememcached'). awk is a little interpreter most often used to take a word number N from every line of input (you can consider it a selector for a column of a text table). In this case, it prints every second word in ps output lines, that means every PID.

If you have any other questions, I'll add answers below.


Here is an explanation of the pieces of code you do not understand:

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"

In the man, near -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 grepps -ef | grep -v grep | grep $( cat $PID )

Print all processes details (ps -ef), exclude the line with grep (grep -v grep) (since you are running grep it will display itself in the process list) and filter by the text found in the file named $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    }

This function will kill all the processes related to memcached slowly and painfully (actually quite the opposite). Above are the explanations.