簡體   English   中英

使用ps和wc檢查服務是否在bash中運行的次數不同

[英]different count using ps and wc to check if service is running in bash

我有一個小腳本來檢查服務是否正在bash中運行

#!/bin/bash

countlines=""$(ps -ef | grep netdata | grep -v grep | wc -l)

echo $countlines >> mylog.log

if [ ${countlines} -lt 3 ];then

sudo /bin/systemctl restart netdata

fi

問題是當我發出ps -ef | grep netdata | grep -v grep | wc -l ps -ef | grep netdata | grep -v grep | wc -l 在命令行上的ps -ef | grep netdata | grep -v grep | wc -l結果始終為3但mylog.log為:

6

[更新:添加了過濾后的ps -ef結果]

forge@reportserver:~ ps -ef | grep netdata
netdata  22308     1  0 08:38 ?        00:00:37 /usr/sbin/netdata -D
netdata  22386 22308  0 08:38 ?        00:00:58 /usr/libexec/netdata/plugins.d/apps.plugin 1
netdata  47045 22308  0 11:38 ?        00:00:02 bash /usr/libexec/netdata/plugins.d/tc-qos-helper.sh 1
forge    52028 27902  0 12:34 pts/8    00:00:00 grep --color=auto netdata

為什么這么不和諧?

分成2個命令以了解原因:

ps_grep_output=$(ps -ef | grep netdata | grep -v grep)

echo "$ps_grep_output" >> mylog.log

countlines=$(wc -l <<< "$ps_grep_output")

echo "$countlines" >> mylog.log

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM