Matching for program names, why so many hits?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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
grep scripting ps
add a comment |Â
up vote
0
down vote
favorite
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
grep scripting ps
As always, read and heed Greg Wooledge's advice on the matter of parsing the process tree, hyperlinked in an answer to a related question at unix.stackexchange.com/questions/377296 and given by other people all over the place here including in answer comments at unix.stackexchange.com/questions/74185 and unix.stackexchange.com/questions/295363 .
â JdeBP
Jan 14 at 16:27
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
grep scripting ps
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
grep scripting ps
edited Jan 14 at 15:37
ilkkachu
49.8k674137
49.8k674137
asked Jan 14 at 15:13
Emilio
1
1
As always, read and heed Greg Wooledge's advice on the matter of parsing the process tree, hyperlinked in an answer to a related question at unix.stackexchange.com/questions/377296 and given by other people all over the place here including in answer comments at unix.stackexchange.com/questions/74185 and unix.stackexchange.com/questions/295363 .
â JdeBP
Jan 14 at 16:27
add a comment |Â
As always, read and heed Greg Wooledge's advice on the matter of parsing the process tree, hyperlinked in an answer to a related question at unix.stackexchange.com/questions/377296 and given by other people all over the place here including in answer comments at unix.stackexchange.com/questions/74185 and unix.stackexchange.com/questions/295363 .
â JdeBP
Jan 14 at 16:27
As always, read and heed Greg Wooledge's advice on the matter of parsing the process tree, hyperlinked in an answer to a related question at unix.stackexchange.com/questions/377296 and given by other people all over the place here including in answer comments at unix.stackexchange.com/questions/74185 and unix.stackexchange.com/questions/295363 .
â JdeBP
Jan 14 at 16:27
As always, read and heed Greg Wooledge's advice on the matter of parsing the process tree, hyperlinked in an answer to a related question at unix.stackexchange.com/questions/377296 and given by other people all over the place here including in answer comments at unix.stackexchange.com/questions/74185 and unix.stackexchange.com/questions/295363 .
â JdeBP
Jan 14 at 16:27
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
0
down vote
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)
Thanks but i need a numeric answer so if the blynk server is not running I can start it via the same bash file
â Emilio
Jan 14 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
. Andpgrep -c
is another way to get a count, etc.
â ilkkachu
Jan 14 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`
â Emilio
Jan 14 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.
â ilkkachu
Jan 14 at 15:51
Sorry about that. It's my first time here.
â Emilio
Jan 14 at 16:01
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
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
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
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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)
Thanks but i need a numeric answer so if the blynk server is not running I can start it via the same bash file
â Emilio
Jan 14 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
. Andpgrep -c
is another way to get a count, etc.
â ilkkachu
Jan 14 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`
â Emilio
Jan 14 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.
â ilkkachu
Jan 14 at 15:51
Sorry about that. It's my first time here.
â Emilio
Jan 14 at 16:01
add a comment |Â
up vote
0
down vote
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)
Thanks but i need a numeric answer so if the blynk server is not running I can start it via the same bash file
â Emilio
Jan 14 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
. Andpgrep -c
is another way to get a count, etc.
â ilkkachu
Jan 14 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`
â Emilio
Jan 14 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.
â ilkkachu
Jan 14 at 15:51
Sorry about that. It's my first time here.
â Emilio
Jan 14 at 16:01
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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)
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)
edited Jan 14 at 15:45
answered Jan 14 at 15:33
ilkkachu
49.8k674137
49.8k674137
Thanks but i need a numeric answer so if the blynk server is not running I can start it via the same bash file
â Emilio
Jan 14 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
. Andpgrep -c
is another way to get a count, etc.
â ilkkachu
Jan 14 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`
â Emilio
Jan 14 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.
â ilkkachu
Jan 14 at 15:51
Sorry about that. It's my first time here.
â Emilio
Jan 14 at 16:01
add a comment |Â
Thanks but i need a numeric answer so if the blynk server is not running I can start it via the same bash file
â Emilio
Jan 14 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
. Andpgrep -c
is another way to get a count, etc.
â ilkkachu
Jan 14 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`
â Emilio
Jan 14 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.
â ilkkachu
Jan 14 at 15:51
Sorry about that. It's my first time here.
â Emilio
Jan 14 at 16:01
Thanks but i need a numeric answer so if the blynk server is not running I can start it via the same bash file
â Emilio
Jan 14 at 15:46
Thanks but i need a numeric answer so if the blynk server is not running I can start it via the same bash file
â Emilio
Jan 14 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.â ilkkachu
Jan 14 at 15:48
@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.â ilkkachu
Jan 14 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`â Emilio
Jan 14 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`â Emilio
Jan 14 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.
â ilkkachu
Jan 14 at 15:51
@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.
â ilkkachu
Jan 14 at 15:51
Sorry about that. It's my first time here.
â Emilio
Jan 14 at 16:01
Sorry about that. It's my first time here.
â Emilio
Jan 14 at 16:01
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Jan 14 at 15:49
Hauke Laging
53.4k1282130
53.4k1282130
add a comment |Â
add a comment |Â
up vote
0
down vote
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
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
add a comment |Â
up vote
0
down vote
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
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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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
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
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
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
answered Jan 18 at 5:29
Bach Lien
1792
1792
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f417043%2fmatching-for-program-names-why-so-many-hits%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
As always, read and heed Greg Wooledge's advice on the matter of parsing the process tree, hyperlinked in an answer to a related question at unix.stackexchange.com/questions/377296 and given by other people all over the place here including in answer comments at unix.stackexchange.com/questions/74185 and unix.stackexchange.com/questions/295363 .
â JdeBP
Jan 14 at 16:27