0

I have a small script to check if a service is running in 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

The problem is when I issue a ps -ef | grep netdata | grep -v grep | wc -l at the command line the result is always 3 but mylog.log is:

6

[update: added filtered ps -ef results]

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

why such a discordance?

Forge
  • 1,587
  • 1
  • 15
  • 36

1 Answers1

0

split into 2 commands to see why:

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
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36