0

Script A :

#!/bin/bash
cmdname=$1
process_num=$(ps -e | grep $cmdname | wc -l)
timestamp=$(date +%s)
echo -e  "$timestamp\t process_num=$process_num"

Script B:

#!/bin/bash
cmdname=$1
process_num=$(ps -ef | grep $cmdname | wc -l)
timestamp=$(date +%s)
echo -e  "$timestamp\t process_num=$process_num"

Suppose there is only one specified process on the system. The result of running script A is 1, but the result of running script B is 4. I don't know why?

hywl51
  • 101
  • 1
  • 1
    your differents are the option '-f' (see also 'man ps') Let's say you search for process "apache" with your first script you will find the process with name "apache" but with your second script you will also find all process from user "apache" – deagh Feb 13 '14 at 13:29
  • Also, note that with the second (`-f` parameter) you will also count the `grep $cmdname` itself. To debug run `ps -ef myprocess` and `ps -e myprocess` outside of script and see what's wrong. If you cannot find, provide us these output. – krisFR Feb 14 '14 at 03:07
  • To deagh, there is not the user named with $cmdname on my system,so I think it should be the cause. – hywl51 Feb 14 '14 at 05:49
  • to user2196728, run ps -ef myprocess outside of script,the result is 2 processes(including grep myprocess itself). – hywl51 Feb 14 '14 at 05:52

1 Answers1

-1

It is because it count the script process also, for suppose you check for a process which is not running for example say, Hello when you check it via your first process it will give 2 but when you run your second script it will show count as 4.

You can check this by running your second script first and first script after that your first script also give count as 4