需要建议编写一个检测一组进程的简单脚本

需要建议编写一个检测一组进程的简单脚本

我需要编写一个简单的脚本来始终在后台运行。它的工作只是监视一种形式的进程,例如 htop 进程。假设 htop 进程正在被触发或运行,然后执行某些操作,当该进程退出时,也执行某些操作。我设法在脚本中使用 while 循环来不断监视后台的此过程。我检查过 CPU 使用率约为 1.4%。现在我不确定这是否不好。我想知道是否有人有其他方法可以做到这一点。谢谢

答案1

这是一个我必须监视后台备份/恢复 RSYNC 进程的脚本。

该脚本有一个“while [true]”循环。然后它对是否退出进行条件测试。

如果要退出,您可以在此处添加逻辑以在退出脚本之前执行您想要的任何任务。

如果要继续,您可以添加命令来跟踪这是否是循环中的第一次,然后相应地执行任务。

使用多少 CPU 取决于在针对指定条件执行另一次扫描之前为每个循环指定的定时延迟。

脚本列表OS_Admin__partitionMirror_Monitor.sh

#!/bin/sh

##########################################################################################################
### $Id: OS_Admin__partitionMirror_Monitor.sh,v 1.3 2022/08/05 03:46:55 root Exp root $
###
### This script is intended to perform an ongoing scan to report when an active RSYNC backup process terminates.
##########################################################################################################

test_STEP1()
{
echo "root        7520    7514 12 20:50 pts/0    00:05:46 rsync --checksum --one-file-system --recursive --outbuf=Line --links --perms --times --group --owner --devices --specials --verbose --out-format=%t|%i|%M|%b|%f| --delete-delay --whole-file --human-readable --protect-args --ignore-errors --msgs2stderr ./ /DB001_F7/
root        7514    7512  0 20:50 pts/0    00:00:25 rsync --checksum --one-file-system --recursive --outbuf=Line --links --perms --times --group --owner --devices --specials --verbose --out-format=%t|%i|%M|%b|%f| --delete-delay --whole-file --human-readable --protect-args --ignore-errors --msgs2stderr ./ /DB001_F7/
root        7512       1 17 20:50 pts/0    00:08:27 rsync --checksum --one-file-system --recursive --outbuf=Line --links --perms --times --group --owner --devices --specials --verbose --out-format=%t|%i|%M|%b|%f| --delete-delay --whole-file --human-readable --protect-args --ignore-errors --msgs2stderr ./ /DB001_F7/" >${TMP}
awk '{ printf("\trsync|%s\n", $0 ) ; }' ${TMP}
echo ""
}

test_STEP2()
{
echo "7520
7514
7512" >${TMP}.pid
awk '{ printf("\t pid |%s\n", $0 ) ; }' ${TMP}.pid
echo ""
}

test_STEP3()
{
echo "7514|7520
7512|7514
1|7512" >${TMP}.ppid
awk '{ printf("\tppid |%s\n", $0 ) ; }' ${TMP}.ppid
echo ""
}



. $Oasis/bin/INCLUDES__TerminalEscape_SGR.bh

BASE=`basename "$0" ".sh" `
TMP="/tmp/tmp.${BASE}.$$"

date | awk '{ printf("\n\t %s\n\n", $0 ) ; }'

if [ "$1" = "--snapshots" ]
then
    SNAP=1
else
    SNAP=0
fi

rm -f ${TMP}
ps -ef 2>&1 | grep -v grep | grep rsync | sort -r >${TMP}
#test_STEP1


if [ ! -s ${TMP} ]
then
    echo "\t RSYNC process is ${redON}not${redOFF} running (or has already ${greenON}terminated${greenOFF}).\n"
    exit 0
fi

awk '{ print $2 }' <${TMP} >${TMP}.pid
#test_STEP2


awk '{ printf("%s|%s\n", $3, $2) }' <${TMP} >${TMP}.ppid
#test_STEP3


for pid in `cut -f1 -d\| ${TMP}.ppid `
do
    PPID=`grep ${pid} ${TMP}.pid `
    PID=`grep '^'${pid} ${TMP}.ppid | cut -f2 -d\| `
    PRNT=`grep '^'${pid} ${TMP}.ppid | cut -f1 -d\| `
    if [ \( -n "${PPID}" \)  -a  \( "${PRNT}" -ne 1 \) ]
    then
        descr="child"
        echo "\t PID ${PID} is RSYNC ${cyanON}${italicON}${descr}${italicOFF}${cyanOFF} process ..."
    else
        descr="MASTER"
        echo "\t PID ${PID} is RSYNC ${yellowON}${descr}${yellowOFF} process ..."
    fi
done

getRsyncProcessStatus()
{
    testor=`ps -ef 2>&1 | awk -v THIS="${PID}" '{ if( $2 == THIS ){ print $0 } ; }' `
    MODE=`echo "${testor}" |
        awk '{ if( $NF ~ /^[/]DB001_F?[/]/ ){ print "2" }else{ print "1" } ; }' 2>>/dev/null ` 
}

getRsyncProcessStatus

if [ ${MODE} -eq 2 ]
then
    echo "\t RSYNC restore process under way ..."
    INTERVAL=60
else
    echo "\t RSYNC backup process under way ..."
    INTERVAL=10
fi

if [ -n "${testor}" ]
then
    echo "\n\t ${testor}\n" | sed 's+--+\n\t\t\t\t\t\t\t\t--+g' | awk '{
        rLOC=index($0,"rsync") ;
        if( rLOC != 0 ){
            sBeg=sprintf("%s", substr($0,1,rLOC-1) ) ;
            sEnd=sprintf("%s", substr($0,rLOC+5) ) ;
            sMid="\033[91;1mrsync\033[0m" ;
            printf("%s%s%s\n", sBeg, sMid, sEnd) ;
        }else{
            print $0 ;
        } ;
    }'
    echo "\n\t Scanning at ${INTERVAL} second intervals ..."
    test ${SNAP} -eq 1 || echo "\t \c"
fi

if [ ${SNAP} -eq 1 ]
then
    while true
    do
        getRsyncProcessStatus
        if [ -z "${testor}" ]
        then
            echo "\n\n\t RSYNC process (# ${PID}) has ${greenON}completed${greenOFF}.\n"
            date | awk '{ printf("\t %s\n\n", $0 ) ; }'
            exit 0
        fi
        jobLog=`ls -tr /site/Z_backup.*.err | tail -1 `
        echo "\t `tail -1 ${jobLog}`"
        sleep ${INTERVAL}
    done 2>&1 | uniq
else
    while true
    do
        getRsyncProcessStatus
        if [ -z "${testor}" ]
        then
            echo "\n\n\t RSYNC process (# ${PID}) has ${greenON}completed${greenOFF}.\n"
            date | awk '{ printf("\t %s\n\n", $0 ) ; }'
            exit 0
        fi
        echo ".\c"
        sleep ${INTERVAL}
    done 
fi


exit 0
exit 0
exit 0

该脚本使用“Bourne Header”脚本,包括__TerminalEscape_SGR.bh

#!/bin/sh

##########################################################################################################
### $Id: INCLUDES__TerminalEscape_SGR.bh,v 1.2 2022/09/03 01:57:31 root Exp $
###
### This includes string variables defined to perform various substitutions for the ANSI Terminal Escape Sequences, i.e. SGR (Select Graphic Rendition subset)
##########################################################################################################

### "\e" is same as "\033"

boldON="\e[1m"
boldOFF="\e[0m"

italicON="\e[3m"
italicOFF="\e[0m"

underlineON="\e[4m"
underlineOFF="\e[0m"

blinkON="\e[5m"
blinkOFF="\e[0m"

cyanON="\e[96;1m"
cyanOFF="\e[0m"

cyanDarkON="\e[36;1m"
cyanDarkOFF="\e[0m"

greenON="\e[92;1m"
greenOFF="\e[0m"

yellowON="\e[93;1m"
yellowOFF="\e[0m"

redON="\e[91;1m"
redOFF="\e[0m"

orangeON="\e[33;1m"
orangeOFF="\e[0m"

blueON="\e[94;1m"
blueOFF="\e[0m"

blueSteelON="\e[34;1m"
blueSteelOFF="\e[0m"

magentaON="\e[95;1m"
magentaOFF="\e[0m"

##########################################################################################################
### Usage Examples:
##########################################################################################################

#   echo "\t RSYNC process is ${redON}not${redOFF} running (or has already ${greenON}terminated${greenOFF}).\n"
#   echo "\t ${PID} is ${cyanON}${italicON}${descr}${italicOFF}${cyanOFF} process ..."
#   echo "\t ${PID} is ${yellowON}${descr}${yellowOFF} process ..."
#   echo "\n\n\t RSYNC process (# ${pid}) has ${greenON}completed${greenOFF}.\n"

##########################################################################################################
### Example of scenario where escape codes are hard-coded; \e was not accepted by awk
##########################################################################################################

#   echo "\n\t ${testor}\n" | sed 's+--+\n\t\t\t\t\t\t\t\t--+g' | awk '{
#       rLOC=index($0,"rsync") ;
#       if( rLOC != 0 ){
#           sBeg=sprintf("%s", substr($0,1,rLOC-1) ) ;
#           sEnd=sprintf("%s", substr($0,rLOC+5) ) ;
#           sMid="\033[91;1mrsync\033[0m" ;
#           printf("%s%s%s\n", sBeg, sMid, sEnd) ;
#       }else{
#           print $0 ;
#       } ;
#   }'

##########################################################################################################
    echo "\n\t Imported LIBRARY:  INCLUDES__TerminalEscape_SGR.bh ..."
##########################################################################################################

RSYNC 未运行时报告的内容的快照:

未检测到 RSYNC 时的行为

RSYNC 运行时报告的内容的快照:

在后台检测到 RSYNC 时的行为

相关内容