2015-02-09 37 views
0

我是在UNIX開發新的,我有以下代碼,我相信是有關​​的控制文件,但我根本不明白,如果任何人都可以向我解釋,我「會非常感激在控制文件創建的UNIX代碼解釋

這是代碼

ARCH_CTRL_LOCK="$RUTA_CFG/servicioPortabilidad.ctl" 
if [ ! -f $ARCH_CTRL_LOCK ]; then 
    (echo "$$" > $ARCH_CTRL_LOCK) 2> /dev/null 
else 
    PID_GUARDADO=`cat $ARCH_CTRL_LOCK` 
    if [ -d /proc/${PID_GUARDADO} ]; then 
    echo "Proceso no iniciado, el archivo $ARCH_CTRL_LOCK existe y su PID esta activo (${PID_GUARDADO}), puede estar ejecutandose otra instancia de este proceso." 
    exit 2 
    fi 
    kill -0 ${PID_GUARDADO} 2> /dev/null 
    echo "" 
    if [ "$?" = "0" ]; then 
    echo "Proceso no iniciado, el archivo $ARCH_CTRL_LOCK existe y su PID responde a senhales (${PID_GUARDADO}), puede estar ejecutandose otra instancia de este proceso." 
    exit 2 
    fi 
    (echo "$$" > $ARCH_CTRL_LOCK) 2> /dev/null 
fi 

謝謝

回答

1
# Store the name of the lock file in a variable, so we can use 
# it repeatedly. 
ARCH_CTRL_LOCK="$RUTA_CFG/servicioPortabilidad.ctl" 

# See if the lock file exists. 
if [ ! -f $ARCH_CTRL_LOCK ]; then 
    # The lock file doesn't exist. 

    # Write my own process ID into the file, creating it 
    (echo "$$" > $ARCH_CTRL_LOCK) 2> /dev/null 

else 
    # The lock file DOES exist. 

    # Get the process ID from the existing lock file. This should be 
    # the ID of the process that created the lock file. 
    PID_GUARDADO=`cat $ARCH_CTRL_LOCK` 

    # Looking for a directory named /proc/PID is a way of checking 
    # whether that process exists. This only works on operating systems 
    # that have a /proc filesystem. Linux always has /proc, but many 
    # other systems (like Mac OS X for example) don't. 

    if [ -d /proc/${PID_GUARDADO} ]; then 
    # The process still exists. 

    # Print an error message. 
    echo "Proceso no iniciado, el archivo $ARCH_CTRL_LOCK existe y su PID esta activo (${PID_GUARDADO}), puede estar ejecutandose otra instancia de este proceso." 

    # Exit with status 2. 
    # All statuses except 0 mean there was an error. 
    exit 2 
    fi 

    # The process doesn't still exist. But the kill command that 
    # follows checks for the process in a different way, that works 
    # on any POSIX system. The kill command exits with status 0 
    # (meaning success) if the process exists, and exists with some 
    # other status (meaning error) if the process doesn't exist. 
    kill -0 ${PID_GUARDADO} 2> /dev/null 

    # The following echo command prints a blank line. It also makes 
    # the shell forget about the exit status of the kill command. 
    echo "" 

    # Now see if the exit status from the last command is zero. I 
    # guess this was intended to check the exit status of kill, but 
    # someone inserted that echo command, which should always exit 
    # with status 0, so the test will always pass! 
    if [ "$?" = "0" ]; then 
    # The exit status of the last command was 0. This should always 
    # be the case. The echo command above should always succeed. 

    # Print an error message. 
    echo "Proceso no iniciado, el archivo $ARCH_CTRL_LOCK existe y su PID responde a senhales (${PID_GUARDADO}), puede estar ejecutandose otra instancia de este proceso." 

    # Exit with status 2. 
    exit 2 
    fi 

    # We can never reach this point, because the echo above should 
    # always succeed, so we always print the error message and exit 
    # in the previous if statement. 

    # Overwrite contents of the lock file with my own process ID. 
    (echo "$$" > $ARCH_CTRL_LOCK) 2> /dev/null 
fi 

Technically,該echo ""命令可能會失敗,但前提是標準出put已關閉,或重定向到完整文件系統上的文件,或以其他方式進行設置,使其不可寫入。這是不可能的。

+0

這是一個很好的教訓,如何解決你不知道的代碼。當你瞭解它的功能時,請添加評論。回過頭來,添加一段代碼作爲一個整體的總結。在這個過程中可能會發現一些錯誤,這些錯誤可以確定您首先調查此問題的原因。然後,從這裏學習,並首先在您的代碼中寫下評論!如果原作者這樣做了,那麼你就不會花費誰知道弄清楚多久。請記住在你之後爲維護這一點的人編寫代碼,他們可能沒有那麼有經驗。 – 2015-02-09 21:43:09