Linux shell script to achieve Singleton pattern

I. Description

About singleton mode, the beginning of some gadgets up and running and then will be prompted to click on the run has been running one instance, I feel quite interesting but did not really care.

Some time ago saw the former leader of a piece of code do not understand is what to do with, colleagues checked the information that is to achieve a singleton, under discussion did not know a single case of mold is a design pattern, specific performance That said above you can only run one instance.

Feedback last week to speak and write shell scripts in the system is to run a good number of processes under investigation and have been found to be yum command causes the entire script can not wait for the lock jammed due, the script will run each time to pull a stuck process . Thus feeling shell scripts should also consider the singleton pattern.

 

Second, if an existing process before the end of the current process Singleton

#!/bin/bash
main(){
    # $ 0 is the filename of the current file
    # If you are running a bash test.sh then $ 0 is the Test. SH 
    # If you are running a bash /tmp/test.sh then $ 0 is the / tmp / the Test. SH 
    # If you only want to run no matter how the file name, you can also basename $ 0 can file_name = $ { 0 ## * / }
    file_name=$0
    # If the number matches the file name of the current process is greater than 1, document examples of not only the current process, exit the current process
    # Cognitively it should be greater than 1, where the cause is greater than 2, said behind
    if [ `pgrep -f ${file_name} | wc -l` -gt 2 ]
    then
        echo "${file_name} process existed, will be exit."
        exit 1
    fi
    # sleep 60
}
main

 

Third, if the process before the end of the process has been continued until the current process Singleton

#!/bin/bash
main(){
    # $ 0 is the current file name
    file_name=$0
    # File all other processes in addition to the current process of
    pid_list=$(pgrep -f ${file_name} | grep -v ^$$\$)
    #-By-process in addition to killing
    for tmp_pid in ${pid_list}
    do
      # Kill in addition to all of its child processes
      pkill -P ${tmp_pid}
      # As the child stuck in a state, you can not receive the default 15) SIGTERM state and then straight exit; sent directly 9) SIGKILL from the kernel to kill
      # pkill -9 -P ${tmp_pid}
      In addition to its own scrapping #
      Pkill # - P in addition to killing the child process will not only kill in addition to its own
      # But there may be blocked after the child process in addition to killing, their subsequent process steps quickly, resulting in the process has no time except kill kill
      kill -9 ${tmp_pid}
    done
}
main

 

Fourth, note the top two modes of explanation

4.1 Why is the second largest section of the if statement is greater than 2 and not greater than 1

From the general perception that we run this script to start a process, the number of the corresponding process if this script is greater than 1 it shows that there are other processes. However, we are in the upper code requirement is greater than 2.

This is because from the observation point of view, running a script to start the process. Then specific time each command will create a child process to run the script execution will end after the child process; still the same as the name of the parent process child process for common commands such as pgrep name. So statistics out of the process will be the number 2, the second largest section of code to accomplish more than 2.

In addition to stress here that the process name pgrep and other "general command", sleep and so is the command of their own. As for what ordinary operator which is temporarily not count special was very clear.

 

4.2 This has no effect on the third section of the code

Since each execute a command will build a child process to execute, and the name of the child and the parent process name is consistent, and the third largest section of the code and is equivalent to all processes other than the current parent process are closed. It would not appear to present the child to kill the parent process also led to the current situation of the parent process has problems? The answer is not.

pgrep passed to grep is the parent process pid, pid pgrep child process pid and other previously run a script that may be present; after grep is passed to kill the child process pid pid pgrep and other previously run a script that may be present.

We also said front, start a child process before executing the command in command after the child process exits, so can not wait for the kill command to kill off the child process pgrep, when it has been passed to grep himself quit.

 

reference:

https://www.mylinuxplace.com/bash-singleton-process/

https://stackoverflow.com/questions/15740481/prevent-process-from-killing-itself-using-pkill

Guess you like

Origin www.cnblogs.com/lsdb/p/12010128.html