Confusion with PS


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Confusion with PS
# 1  
Old 09-27-2012
Confusion with PS

Hello All,

I have a problem in counting number of process getting run with my current script name..

Here it is

Code:
ps -ef | grep $0 | grep -v grep

This display just one line with the PID, PPID and other details when i print it in the script.

But when I want to count the numbers in my script , just like below, it comes out as 2... Whereas only one process is running..

Code:
count=$(ps -ef | grep $0 | grep -v grep | wc -l)
echo $count

My mind completely gone blank, help me here..
Help me how I will get my script to print the correct number of Process running with its name. Thanks.
# 2  
Old 09-27-2012
Quote:
Originally Posted by sathyaonnuix
Code:
ps -ef | grep $0 | grep -v grep

Just one question. from where you are getting $0?
# 3  
Old 09-27-2012
Thats my Script name, this command line is placed inside my script...
# 4  
Old 09-27-2012
Quote:
Originally Posted by pamu
Just one question. from where you are getting $0?
If I understand correctly, he runs these commands from inside a script.

However, see my script, named "file":

Code:
#!/bin/bash
echo $0
ps axu |head -n 1 # this is to see ps headers
ps axu | grep $0 | grep -v grep | tee /dev/tty |wc -l
ps axu | grep $0 | grep -v grep  |tee /dev/tty |wc -l

If I run it, most of the time I get:
Code:
lem@biggy:/tmp$ ./file
./file
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
lem      25757  0.0  0.0  18584  1432 pts/3    S+   11:44   0:00 /bin/bash ./file
1
lem      25757  0.0  0.0  18584  1432 pts/3    S+   11:44   0:00 /bin/bash ./file
1

Very few times I get instead:
Code:
lem@biggy:/tmp$ ./file
./file
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
lem      25770  0.0  0.0  18584  1428 pts/3    S+   11:44   0:00 /bin/bash ./file
1
lem      25770  0.0  0.0  18584  1428 pts/3    S+   11:44   0:00 /bin/bash ./file
lem      25779  0.0  0.0  18584   432 pts/3    R+   11:44   0:00 /bin/bash ./file
2

The first ps always gives me one process, the second ps seldom gives two.
--
Bye
# 5  
Old 09-27-2012
I found out the problem Smilie Since we give wc -l, this gets a new line. And hence that has got counted in it.

I changed that to
Code:
ps -ef | grep $0 | grep -cv grep

Now it works cool. Thanks Pamu and Lem.
# 6  
Old 09-27-2012
Quote:
Originally Posted by sathyaonnuix
Now it works cool.
Sadly, i don't think so. Smilie
--
Bye
# 7  
Old 09-27-2012
All of the problems witnessed above are the result of the shell forking to create new processes to run a pipeline: ps, grep, wc, another shell for the command substition (in the case of the original post). All of these processes initially begin as a new shell with the same name, $0, as the parent shell.

If ps scans the process list before any of these shells have had a chance to exec and become the utility it will become, the result is multiple instances of $0.

The solution is trivial and widely available: pgrep

Quote:
Originally Posted by sathyaonnuix
I found out the problem Smilie Since we give wc -l, this gets a new line. And hence that has got counted in it.

I changed that to
Code:
ps -ef | grep $0 | grep -cv grep

Now it works cool.
Your analysis is incorrect. You're just getting lucky with timing. In your original wc version, you were unlucky with timing. Nothing has been fixed.

The reason pgrep works reliably is because it's only one command and it doesn't run until after the shell that was forked to create it has exec'd.

Unless the system lacks pgrep and pkill, there's no need to resort to the ps ... | grep/awk ... silliness.

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confusion in hash

Hi folks, If a declare a direct hash , then the hash element works fine. my %test = ("test",1); print %test; print "\n"; Here in the above, the name of the hash is predeclared... Suppose now I need to create the hash elements dynamically in the for loop. $test="hash"; my... (1 Reply)
Discussion started by: scriptscript
1 Replies

2. Shell Programming and Scripting

confusion in use of exit 0

hi i am new to shell scripting. i was going thru the part option and arguments. on this section i fail to understand the use of exit 0 in below example . #!/bin/sh USAGE="Usage: $0 " case "$1" in -t) TARGS="-tvf $2" ;; -c) TARGS="-cvf $2.tar $2" ;; *) echo "$USAGE" exit 0 ;; esac... (13 Replies)
Discussion started by: scriptor
13 Replies

3. Homework & Coursework Questions

Server Confusion

I don't even know where to start with this one. There is so much out there about different aspects of this. I am starting with a basic Ubuntu 11.04 install. Do I need to configure a DNS? I am a little confused about that. What do I need to do for a domain name? I have followed various tutorials,... (1 Reply)
Discussion started by: polyglot0727
1 Replies

4. Programming

shmget confusion?????

Hi friends, This is a small program built on the concept of shared memory. The producer is a separate program and process, and the consumer is a seperate program and process. Both are executed under the same user account. The producer takes some string from the user and adds that string to the... (1 Reply)
Discussion started by: gabam
1 Replies

5. Shell Programming and Scripting

conditional confusion

Hell Unix.com Community: I am working on a personal project using yad v0.12.4 (zenity fork) and have hit a wall on how to show a progress bar while my function is processing. I have been all over the ABS Guide, googled 21 Linux-specific sites that I revere. I even asked on the yad-common... (4 Replies)
Discussion started by: Habitual
4 Replies

6. Programming

C fork Confusion :-?

Hi, I was trying to learn forking in C in UNIX. Somehow i still haven't been able to get the concept well. I mean, i do understand that fork creates an exact replica of the parent (other than the fact that parent gets the process id of the child and child gets 0 when fork is called). This is the... (2 Replies)
Discussion started by: ralpheno
2 Replies

7. UNIX for Dummies Questions & Answers

crontab confusion

I come across an entry in cron which is in such: 0 * * * * What is the first 0 indicating? 0 minute? meaning a script cron as such will run every minute? :confused: (2 Replies)
Discussion started by: user50210
2 Replies

8. UNIX for Dummies Questions & Answers

'tr' confusion

Good day, everyone! Could anybody explain me the following situation. If I'm running similar script: Var="anna.kurnikova" Var2="Anna Kurn" echo $Var | tr -t "$Var" "$Var2" Why the output is : anna KurniKova instead of Anna Kurnikova? :confused: Thank you in advance for any... (2 Replies)
Discussion started by: Nafanja
2 Replies

9. UNIX for Dummies Questions & Answers

unix confusion

:confused: some one please tell me where i can possibly find out what is unix 10.2 and the basic system functions of it is. I really need help! (1 Reply)
Discussion started by: tribb24
1 Replies
Login or Register to Ask a Question