항상 백그라운드에서 실행되도록 간단한 스크립트를 작성해야 합니다. 그 임무는 단순히 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가 실행될 때 보고하는 내용의 스냅샷: