View previous topic :: View next topic |
Author |
Message |
Vieri l33t
Joined: 18 Dec 2005 Posts: 881
|
Posted: Mon Feb 25, 2013 7:44 am Post subject: [SOLVED] count number of processes of current bash script |
|
|
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 |
|
|
py-ro Veteran
Joined: 24 Sep 2002 Posts: 1734 Location: Velbert
|
Posted: Mon Feb 25, 2013 11:29 am Post subject: |
|
|
Because grep finds itself. |
|
Back to top |
|
|
Vieri l33t
Joined: 18 Dec 2005 Posts: 881
|
Posted: Mon Feb 25, 2013 12:42 pm Post subject: |
|
|
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 |
|
|
Genone Retired Dev
Joined: 14 Mar 2003 Posts: 9530 Location: beyond the rim
|
Posted: Mon Feb 25, 2013 2:01 pm Post subject: |
|
|
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 |
|
|
aCOSwt Bodhisattva
Joined: 19 Oct 2007 Posts: 2537 Location: Hilbert space
|
Posted: Mon Feb 25, 2013 2:50 pm Post subject: Re: count number of processes of current bash script |
|
|
Vieri wrote: | Why am I getting 2? |
And the answer is : Because you really get two processes which comm name is test.sh !
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 |
|
|
Vieri l33t
Joined: 18 Dec 2005 Posts: 881
|
Posted: Tue Feb 26, 2013 7:31 am Post subject: |
|
|
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 |
|
|
Naib Watchman
Joined: 21 May 2004 Posts: 6051 Location: Removed by Neddy
|
Posted: Tue Feb 26, 2013 10:16 pm Post subject: |
|
|
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 |
|
|
|