# Confusion with PS

**URL:** <https://community.unix.com/t/confusion-with-ps/317330>\
**Category:** Shell Programming and Scripting\
**Created:** [September 27, 2012, 4:32am UTC](https://community.unix.com/t/confusion-with-ps/317330 "2012-09-27T04:32:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![sathyaonnuix](https://community.unix.com/user_avatar/community.unix.com/sathyaonnuix/32/2703_2.png) [@sathyaonnuix](https://community.unix.com/u/sathyaonnuix)\
**Post date:** [September 27, 2012, 4:32am UTC](https://community.unix.com/t/confusion-with-ps/317330/1 "2012-09-27T04:32:42Z")

</div>

Hello All,

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

Here it is

```nohighlight
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..

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![pamu](https://community.unix.com/letter_avatar/pamu/32/5_5575768a8748004e209b776fc1b2916d.png) [@pamu](https://community.unix.com/u/pamu)\
**Post date:** [September 27, 2012, 5:27am UTC](https://community.unix.com/t/confusion-with-ps/317330/2 "2012-09-27T05:27:23Z")

</div>

> [@sathyaonnuix](#):
>
> ```plaintext
> ps -ef | grep $0 | grep -v grep
> 
> ```

Just one question. from where you are getting `$0` ?

---

<div class="post-metadata">

**Author:** ![sathyaonnuix](https://community.unix.com/user_avatar/community.unix.com/sathyaonnuix/32/2703_2.png) [@sathyaonnuix](https://community.unix.com/u/sathyaonnuix)\
**Post date:** [September 27, 2012, 5:48am UTC](https://community.unix.com/t/confusion-with-ps/317330/3 "2012-09-27T05:48:57Z")

</div>

Thats my Script name, this command line is placed inside my script...

---

<div class="post-metadata">

**Author:** ![Lem](https://community.unix.com/letter_avatar/lem/32/5_5575768a8748004e209b776fc1b2916d.png) [@Lem](https://community.unix.com/u/Lem)\
**Post date:** [September 27, 2012, 5:50am UTC](https://community.unix.com/t/confusion-with-ps/317330/4 "2012-09-27T05:50:53Z")

</div>

> [@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":

```plaintext
#!/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:

```plaintext
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:

```plaintext
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

---

<div class="post-metadata">

**Author:** ![sathyaonnuix](https://community.unix.com/user_avatar/community.unix.com/sathyaonnuix/32/2703_2.png) [@sathyaonnuix](https://community.unix.com/u/sathyaonnuix)\
**Post date:** [September 27, 2012, 6:23am UTC](https://community.unix.com/t/confusion-with-ps/317330/5 "2012-09-27T06:23:40Z")

</div>

I found out the problem 🙂 Since we give wc -l, this gets a new line. And hence that has got counted in it.

I changed that to

```nohighlight
ps -ef | grep $0 | grep -cv grep

```

Now it works cool. Thanks Pamu and Lem.

---

<div class="post-metadata">

**Author:** ![Lem](https://community.unix.com/letter_avatar/lem/32/5_5575768a8748004e209b776fc1b2916d.png) [@Lem](https://community.unix.com/u/Lem)\
**Post date:** [September 27, 2012, 6:49am UTC](https://community.unix.com/t/confusion-with-ps/317330/6 "2012-09-27T06:49:17Z")

</div>

> [@sathyaonnuix](#):
>
> Now it works cool.

Sadly, i don't think so. ☹  
--  
Bye

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/5_5575768a8748004e209b776fc1b2916d.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [September 27, 2012, 8:38am UTC](https://community.unix.com/t/confusion-with-ps/317330/7 "2012-09-27T08:38:43Z")

</div>

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`

> [@sathyaonnuix](#):
>
> I found out the problem 🙂 Since we give wc -l, this gets a new line. And hence that has got counted in it.
> 
> I changed that to
> 
> ```plaintext
> 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

---

<div class="post-metadata">

**Author:** ![sathyaonnuix](https://community.unix.com/user_avatar/community.unix.com/sathyaonnuix/32/2703_2.png) [@sathyaonnuix](https://community.unix.com/u/sathyaonnuix)\
**Post date:** [September 27, 2012, 10:55am UTC](https://community.unix.com/t/confusion-with-ps/317330/8 "2012-09-27T10:55:01Z")

</div>

Hey Alister,

Yes I WAS lucky to get that desired ouput. After a while, I found out am unlucky.. It again started creating that mess. Also the problem is my system doesn't contain pgrep ☹ I found out another way to accomplish my result.

If there is any other way that we can acquire the result with grep, wc or other silly commands, please post here..

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/5_5575768a8748004e209b776fc1b2916d.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [September 27, 2012, 12:45pm UTC](https://community.unix.com/t/confusion-with-ps/317330/9 "2012-09-27T12:45:00Z")

</div>

Exactly which version of which operating system you are using?

It will help to know exactly what you're trying to accomplish. Show us the shell script and explain what it's supposed to do.

Regards,  
Alister

---

<div class="post-metadata">

**Author:** ![Lem](https://community.unix.com/letter_avatar/lem/32/5_5575768a8748004e209b776fc1b2916d.png) [@Lem](https://community.unix.com/u/Lem)\
**Post date:** [September 27, 2012, 1:16pm UTC](https://community.unix.com/t/confusion-with-ps/317330/10 "2012-09-27T13:16:14Z")

</div>

> [@](#):
>
> If there is any other way that we can acquire the result with grep, wc or other silly commands, please post here..

Hint:

```plaintext
ps -ef >tmpfile
count=$(grep $0 tmpfile |grep -v grep |wc -l)
...

```

--  
Bye

EDIT: LOL, obviously in this case there's no need to purge "grep" strings from grep output.

---

<div class="post-metadata">

**Author:** ![sathyaonnuix](https://community.unix.com/user_avatar/community.unix.com/sathyaonnuix/32/2703_2.png) [@sathyaonnuix](https://community.unix.com/u/sathyaonnuix)\
**Post date:** [September 28, 2012, 1:40am UTC](https://community.unix.com/t/confusion-with-ps/317330/11 "2012-09-28T01:40:52Z")

</div>

@Lem: you are simply awesome.

@Alister: I have a cron which runs my script every 1 minute. If any error identified by my script, it runs another program which takes a while say 4 mins. Until it gets completed, my Cron should not allow the Script to run on the next minute.. So i thought of counting the number of PS of that script name will fix that problem. Thanks to Lem for that Solution.

What I have built is as below:

```nohighlight
pd=$(head -1 pid.check)
ps -ef | grep $pd | grep -v grep 
if [$? -eq 0]; then
exit 0
fi

pid1=$$
echo "$pid1" > pid.check

```

Shout me if the above fails at any condition, I will correct my code. Thanks

---

<div class="post-metadata">

**Author:** ![methyl](https://community.unix.com/letter_avatar/methyl/32/5_5575768a8748004e209b776fc1b2916d.png) [@methyl](https://community.unix.com/u/methyl)\
**Post date:** [September 28, 2012, 6:01am UTC](https://community.unix.com/t/confusion-with-ps/317330/12 "2012-09-28T06:01:15Z")

</div>

Needs a "first time" condition. Also, if you know the pid just look for the one pid. You can dump the output from ps as you only need to know if the process is still running.

```nohighlight
# Has previous script finished?
if [-f pid.check]
then
       pd=$(head -1 pid.check)
       ps -fp $pd 1>/dev/null
       if [$? -eq 0]
       then
              exit 0
       fi
fi
# Record pid of current script
pid1=$$
echo "$pid1" > pid.check
### PROCESSING GOES HERE

```

It would be better to specify a directory (e.g. /var/tmp) for pid.check file or it will be created in the home directory of the owner of the crontab.
