LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script, which should diff between ps outputs? (https://www.linuxquestions.org/questions/programming-9/shell-script-which-should-diff-between-ps-outputs-789502/)

m4rtin 02-16-2010 11:05 AM

shell script, which should diff between ps outputs?
 
I would like to make a one-liner, which diff's "ps aux" output before and after killing a process. So basically it should be a combination of fallowing commands: 1) diff -u 2) ps aux | awk '{print $2}' 3) kill `ps aux | grep [c]ron | awk '{print $2}'` 4) ps aux | awk '{print $2}'

Any ideas how to achieve this? Something like this:

Code:

diff $( kill `ps aux | grep [c]ron | awk '{print $2}'` && ps aux | awk '{print $2}' ) $( ps aux | awk '{print $2}' )
I don't have pgrep or pkill available. Any ideas how to achieve this? :rolleyes:

pixellany 02-16-2010 11:55 AM

What happens when you run that script?

Note that grep returns the whole line that contains the specified pattern. You probably need "grep -o"

How is "grep [c]ron" different from "grep cron" ?

firstfire 02-18-2010 05:18 AM

Hi.

If you want to compare output of two processes, try so called "process substitution" (available in BASH on some systems. see man bash):
Code:

diff <(process1) <(process2)
For example:
Code:

/tmp$ > test
/tmp$ diff <(ls -1) <(sleep 1; rm test ; ls -1)
29d28
< test

It seems that both processes run simultaneously (at lest on my computer). That's why I use 'sleep 1' here.

BTW: I found that the difference between
Code:

ps aux | grep [c]ron
and the same without square brackets is that the second (without []) will show 'grep cron' process itself. Though I don't yet understand why.

GazL 02-18-2010 07:29 AM

Quote:

Originally Posted by pixellany (Post 3865814)

How is "grep [c]ron" different from "grep cron" ?

The problem with just using a 'grep cron' is that the regex will also match the string within the command line of the process doing the grep which also contains the string 'cron'. An inexperienced coder will probably fix this by doing something like

Code:

ps aux | grep cron | grep -v grep
... but that is inefficient as it requires an additional process in the pipline.

More experienced shell script writers will use the square bracket trick to avoid this. By using the square brackets (which will match any one of the characters included in the list) with just a single character, the 'cron' on the grep in the process list becomes 'c]ron' and no longer matches the regex.


I'm not entirely sure what the original poster is trying to achieve with the diff. If he's just attempting to confirm that the process has been killed then there are probably better ways of doing what he wants. A diff of 'ps aux' before and after is fraught with problems, even when limited to only the pid field. i.e. other reasons than the kill may change the process list.

Instead, I'd be inclined to be more specific and do something like a:
Code:

bash-3.1$ ps -eo pid,stime,cmd |grep '^.\{12\}/usr/sbin/crond'
 2499 11:14 /usr/sbin/crond -l10

... but without knowing what it is the OP is attempting to do, that may not be what he's looking for either.


All times are GMT -5. The time now is 09:21 AM.