Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] count number of processes of current bash script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Vieri
Guru
Guru


Joined: 18 Dec 2005
Posts: 374

PostPosted: Mon Feb 25, 2013 7:44 am    Post subject: [SOLVED] count number of processes of current bash script Reply with quote

Hi,

I've just come across a weird behavior in BASH. I'm sure there's a logical explanation for this and I hope someone here can help me understand it.

Here's the sample script:

Code:

# cat test.sh
#!/bin/bash

num_procs=$( ps -ae | grep -c "test.sh\$" )
num_procs2=$( ps -ae | grep "[t]est.sh\$" | wc -l )

ps -ae | grep "test.sh\$"
echo "Num processes: ${num_procs}"
echo "Num processes 2: ${num_procs2}"

if [ "x${num_procs}" = "x1" ]; then
    echo "equal (txt)"
else
    echo "not equal (txt)"
fi

if [ $((num_procs)) -eq 1 ]; then
    echo "equal"
else
    echo "not equal"
fi

exit 0


Here's the output:

Code:

# ./test.sh
10783 pts/0    00:00:00 test.sh
Num processes: 2
Num processes 2: 2
not equal (txt)
not equal


I'm expecting num_procs to be 1, not 2.

Why am I getting 2?

Thanks,

Vieri


Last edited by Vieri on Tue Feb 26, 2013 7:34 am; edited 1 time in total
Back to top
View user's profile Send private message
py-ro
Veteran
Veteran


Joined: 24 Sep 2002
Posts: 1498
Location: St. Wendel

PostPosted: Mon Feb 25, 2013 11:29 am    Post subject: Reply with quote

Because grep finds itself.
Back to top
View user's profile Send private message
Vieri
Guru
Guru


Joined: 18 Dec 2005
Posts: 374

PostPosted: Mon Feb 25, 2013 12:42 pm    Post subject: Reply with quote

Not if the code is
Code:
ps -ae | grep "[t]est.sh\$" | wc -l
, as above.

Also, if I run
Code:
ps -ae | grep "test.sh\$"
within the bash script, the output is
Code:
10783 pts/0    00:00:00 test.sh
and there's no grep in sight...
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9013
Location: beyond the rim

PostPosted: Mon Feb 25, 2013 2:01 pm    Post subject: Reply with quote

Random guess is that the $() subshell affects the result. Could try running your "control grep" in a subshell too to verify, e.g. `echo $(ps -ae | grep 'test.sh$')` instead of `ps -ae | grep "test.sh\$"`
Back to top
View user's profile Send private message
aCOSwt
Moderator
Moderator


Joined: 19 Oct 2007
Posts: 2537
Location: Hilbert space

PostPosted: Mon Feb 25, 2013 2:50 pm    Post subject: Re: count number of processes of current bash script Reply with quote

Vieri wrote:
Why am I getting 2?

And the answer is : Because you really get two processes which comm name is test.sh !
8O
Code:

num_procs2=$( ps -ae | grep "[t]est.sh\$" | wc -l )

In order to execute this, test.sh will need to fork, the child's comm name being... test.sh !
So when this ps executes, there are actually 2 processes which comm name is = test.sh

If you want to put an evidence on this then, instead of ps -ae.... launch whatever program that would wait before returning a result.
Launch this script on one tty and on another one, do a ps !
_________________
Back to top
View user's profile Send private message
Vieri
Guru
Guru


Joined: 18 Dec 2005
Posts: 374

PostPosted: Tue Feb 26, 2013 7:31 am    Post subject: Reply with quote

Got it! Thanks!

So my bash script spawns another bash process with the same name when called with $().

Crystal clear now.
Thanks again.
Back to top
View user's profile Send private message
Naib
Advocate
Advocate


Joined: 21 May 2004
Posts: 4159
Location: Removed by Neddy

PostPosted: Tue Feb 26, 2013 10:16 pm    Post subject: Reply with quote

just to add my 2cent

http://mywiki.wooledge.org/ProcessManagement


why ps aux | grep [f]oo will return the pid (as well as other things) of process foo (and not the grep command), it is far from the ideal solution to process manage in bash.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum