我的进程 grep 返回多个具有相似名称的字符串

我的进程 grep 返回多个具有相似名称的字符串

我有一个脚本可以检查 Linux 服务器上的进程。

例如,我们有一个ebond-dbreader-bbg在服务器 1 上命名的组件,还有一个ebond-dbreader-bbg-cbrt在服务器 1 上命名的组件。

当我们运行以下命令时,它返回计数 2,而它应该是计数 1,但它bond-dbreader-bbg在 name 内部变得混乱ebond-dbreader-bbg-cbrt

脚本减去变量:

ps -ef | grep ebond-dbreader-bbg | grep -v grep| grep -Ev " pts/[0-9]{1,4}" |wc -l

我得到的输出是2

这是因为如果你做了一个

ps -ef | grep ebond-dbreader-bbg

您会得到两个进程,但其中一个具有不同的名称,请参见下文:

[ebondgen@ebondfm1i20 bin]$ ps -ef | grep ebond-dbreader-bbg | grep -v grep| grep -Ev " pts/[0-9]{1,4}" |wc -l
2


[ebondgen@ebondfm1i20 bin]$ ps -ef | grep ebond-dbreader-bbg
ebond    127056      1  2 00:15 ?        00:25:14 /opt/java1.8/bin/java -Xms2048M -Xmx2048M -Dprogram.name=ebond-dbreader-bbg-cbrt -Dlog4j.configuration=file:resource/log4j.xml -Dproperty.file=resource/dbr.properties -DzooKeeperHosts=ebond-zookeeper1-uat.zit.commerzbank.com:2181 -DapplicationId=ebond-dbreader-bbg-cbrt-0 -server -XX:+UseConcMarkSweepGC -XX:+AlwaysPreTouch -XX:+PrintTenuringDistribution -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintAdaptiveSizePolicy -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+UseGCLogFileRotation -XX:GCLogFileSize=1000K -XX:NumberOfGCLogFiles=5 -Xloggc:/var/eBondTS/log/ebond-dbreader-bbg-cbrt.gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/eBondTS/log/ebond-dbreader-bbg-cbrt.hprof -cp :/opt/eBondTS/ebond-dbreader-bbg-cbrt/20191114135302_01000040/resource:/opt/eBondTS/ebond-dbreader-bbg-cbrt/20191114135302_01000040/lib/* -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=13075 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=true -Dcom.sun.management.jmxremote.access.file=/opt/eBondTS/ebond-certificates/current/resource/jmxWrite.access -Djava.security.auth.login.config=/opt/eBondTS/ebond-certificates/current/resource/ldap.conf -Dcom.sun.management.jmxremote.login.config=LdapSSLNoVerify -Djavax.net.ssl.keyStore=/opt/eBondTS/serverkey/keystore -Djavax.net.ssl.keyStorePassword=T55EJqVOdPLyVdnYpLMV -Djavax.net.ssl.trustStore=/opt/eBondTS/ebond-certificates/current/resource/truststore -Djavax.net.ssl.trustStorePassword=T55EJqVOdPLyVdnYpLMV -Dcom.sun.management.jmxremote.debug=true -Dbuild.number=1.0.40-01000040 -Dsun.net.inetaddr.ttl=0 com.drkw.dbreader.DbReader


ebond    127572      1  2 00:15 ?        00:25:39 /opt/java1.8/bin/java -Xms2048M -Xmx2048M -Dprogram.name=ebond-dbreader-bbg -Dlog4j.configuration=file:resource/log4j.xml -Dproperty.file=resource/dbr.properties -DzooKeeperHosts=ebond-zookeeper1-uat.zit.commerzbank.com:2181 -DapplicationId=ebond-dbreader-bbg-0 -server -XX:+UseConcMarkSweepGC -XX:+AlwaysPreTouch -XX:+PrintTenuringDistribution -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintAdaptiveSizePolicy -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+UseGCLogFileRotation -XX:GCLogFileSize=1000K -XX:NumberOfGCLogFiles=5 -Xloggc:/var/eBondTS/log/ebond-dbreader-bbg.gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/eBondTS/log/ebond-dbreader-bbg.hprof -cp :/opt/eBondTS/ebond-dbreader-bbg/20191114135312_01000040/resource:/opt/eBondTS/ebond-dbreader-bbg/20191114135312_01000040/lib/* -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=13074 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=true -Dcom.sun.management.jmxremote.access.file=/opt/eBondTS/ebond-certificates/current/resource/jmxWrite.access -Djava.security.auth.login.config=/opt/eBondTS/ebond-certificates/current/resource/ldap.conf -Dcom.sun.management.jmxremote.login.config=LdapSSLNoVerify -Djavax.net.ssl.keyStore=/opt/eBondTS/serverkey/keystore -Djavax.net.ssl.keyStorePassword=T55EJqVOdPLyVdnYpLMV -Djavax.net.ssl.trustStore=/opt/eBondTS/ebond-certificates/current/resource/truststore -Djavax.net.ssl.trustStorePassword=T55EJqVOdPLyVdnYpLMV -Dcom.sun.management.jmxremote.debug=true -Dbuild.number=1.0.40-01000040 -Dsun.net.inetaddr.ttl=0 com.drkw.dbreader.DbReader
ebondgen 134027  91663  0 19:12 pts/5    00:00:00 grep ebond-dbreader-bbg

我需要这个命令

ps -ef | grep ebond-dbreader-bbg | grep -v grep| grep -Ev " pts/[0-9]{1,4}" |wc -l

仅返回 1 而不是 2,因为 2 是不正确的,有谁知道如何解决这个问题?

带有变量的脚本

#!/bin/bash
outputDir=/tmp
touch $outputDir/checkOutput.txt

currentServerRole=$(/opt/eBondTS/ebond-server-scripts/current/bin/getServerRole.sh)
excludedComponents=('ebond-devops-scripts' 'ebond-checksum-validator' 'ebond-platform-webtool' 'ebond-deployment-guineapigone' 'ebond-deployment-guineapigtwo' 'ebond-deployment-guineapigthree' 'ebond-declarative-release' 'ebond-rhl-installer' 'ebond-web-broker' 'ebond-curvesubscriber' 'ebond-ampsperformancepublisher' 'ebond-ampsperformancesubscriber' 'ebond-diva-latencycheck' 'ebond-gal-latencycheck' 'ebond-neonpricerec-all' 'ebond-neonpricerec-banks' 'ebond-neonpricerec-chf' 'ebond-neonpricerec-covered' 'ebond-neonpricerec-credit' 'ebond-neonpricerec-derivatives' 'ebond-neonpricerec-emlm1' 'ebond-neonpricerec-emlm2' 'ebond-neonpricerec-other' 'ebond-neonpricerec-rates' 'ebond-neonpricerec-strips' 'ebond-neonpricerec-ust' 'razor-diva-test' 'ebond-reconcile-tool-tp-icap' 'ebond-reconcile-tool-bgc' 'ebond-reconcile-tool-tradition' 'ebond-zookeeper' 'ebond-ebond2bbg-reconciler' 'ebond-overnight-rates-subscriber' 'ebond-curvesubscriber' 'ebond-instrumentstatic-subscriber' 'ebond-userstatic-subscriber' 'ebond-web-broker')
ebond_components=$(grep runInServerRoles /opt/eBondTS/ebond*/current/etc/deployment.properties| awk -F'/' '{print $4}')
razor_components=$(grep -s runInServerRoles /opt/eBondTS/razor*/current/etc/deployment.properties| awk -F'/' '{print $4}')
all_components=$(echo ${ebond_components} ${razor_components})
echo "Id,ComponentName,CellName,ConfiguredYmlValue,BoxRunLevel,ExpectedToRun,CurrentProcessCount,CheckStatus" >> $outputDir/finalOutput.txt

for componentName in $all_components
do
expectedToRunIn=$(grep -s runInServerRoles  /opt/eBondTS/$componentName/current/etc/deployment.properties | sed 's,runInServerRoles=,,')
configuration=$(grep -s configuration  /opt/eBondTS/$componentName/current/etc/deployment.properties| sed 's,configuration=,,')
pkgType=$(grep -s packageType /opt/eBondTS/$componentName/current/etc/deployment.properties | sed 's,packageType=,,')
cellName=$(grep -s cellTeamName  /opt/eBondTS/$componentName/current/etc/deployment.properties| sed 's,cellTeamName=,,')
checkStatus="NOT-OK-CantCheck!!!!"

expectedCount="No"
if [ "$currentServerRole" == "$expectedToRunIn" ]; then
    expectedCount="Yes"
fi
if [ "$expectedToRunIn" == "all" ]; then
    expectedCount="Yes"
fi
if [[ pkgType = java ]]; then
        processes=$(ps -ef | grep "=$componentName " | grep -v grep| grep -Ev " pts/[0-9]{1,4}" |wc -l)
else
        processes=$(ps -ef | grep $componentName | grep -v grep| grep -Ev " pts/[0-9]{1,4}" |wc -l)
fi

答案1

IIUC,你想要:

ps -ef | grep '[e]bond-dbreader-bbg ' | wc -l

您不需要grep -v grep该解决方案。

相关内容