You get a higher number from the grep
because there are more programs running that match the pattern.
If you look at the output of grep
(without the -c
), you'll see which lines in the output of ps
match. E.g. if I make a script like this, I get three:
$ cat check_blynk.sh
#!/bin/bash
foo=$(/bin/ps a | /bin/grep "blynk")
echo "$foo"
$ bash check_blynk.sh
28874 pts/11 S+ 0:00 bash check_blynk.sh
28875 pts/11 S+ 0:00 bash check_blynk.sh
28877 pts/11 S+ 0:00 /bin/grep blynk
That's one for the grep
, since the pattern it uses matches itself, one for script that just so happens to contain the same word in its name, and another for the fact that the ps | grep
is running in a subshell, i.e. another copy of the shell. (I'm not sure what the fourth one would be.)
You might want to use something like pgrep -c blynk
instead, assuming blynk
is the name of program file. pgrep
by default checks the actual file name of the running program, instead of the whole command line. (With -f
it checks the command line, but the you'll match the Bash script again)