0

In my box, the code /bin/ps -aux | /bin/grep -c "blynk"returns a 1 because blynk server is not running.

However, when the same code is ran in a bash file, it returns 4. How does that happen?

#!/bin/sh stat=`/bin/ps -aux | /bin/grep -c "blynk"` if [ $stat -lt "2" ]; then echo not running else echo running date fi 
1

3 Answers 3

1

You have to prevent grep from finding itself. An easy way to do that is this:

/bin/ps -aux | /bin/grep -c "[b]lynk" 

That way grep searches for blynk without having it in its command line. Or you prevent grep from running at the same time:

/bin/ps -aux >ps.txt /bin/grep -c "[b]lynk" ps.txt 

Of course, it makes sense to not grep command lines at all because you would also find editors which were opened with a file README.blynk.

Thus it is better to use pgrep or modify the ps output, limit it to command names or command paths.

0

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)

5
  • Thanks but i need a numeric answer so if the blynk server is not running I can start it via the same bash file Commented Jan 14, 2018 at 15:46
  • @Emilio, I though you asked "how" it happens that the result is what it is. You'll see exactly what they are, if you run the grep without -c. And pgrep -c is another way to get a count, etc. Commented Jan 14, 2018 at 15:48
  • like: #!/bin/sh # #Watchdog script for blynk server # stat=/bin/ps -aux | /bin/grep -c "blynk"` server="/volume1/blynk/server-0.29.5-java8.jar" if [ $stat -lt "2" ]; then /var/packages/Java8/target/j2sdk-image/bin/java -Djava.net.preferIPv4Stack=true -jar /volume1/homes/admin/$server -serverConfig /volume1/homes/admin/blynk_config/server.properties -dataFolder /volume1/homes/admin/blynk_data & fi` Commented Jan 14, 2018 at 15:48
  • @Emilio, please don't put code in comments like that, the formatting doesn't work and the result looks awful. If you're wondering how to change the grep pattern to only match the process you want, it would help if you stated that in your question, and also add the exact command line you are trying to match. That way it would be easier to identify where false hits come from. Commented Jan 14, 2018 at 15:51
  • Sorry about that. It's my first time here. Commented Jan 14, 2018 at 16:01
0

A bash function to check if a process of a command is running, defunct (zombie, dead) excluded:

_isRunning() { ps -o comm= -C "$1" 2>/dev/null | grep -x "$1" >/dev/null 2>&1 } 

Note: ps reports defunct processes too, so, grep -x is used

Example usages:

if _isRunning blynk; then echo not running else echo running date fi 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.