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}' ) |
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" ? |
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) Code:
/tmp$ > test BTW: I found that the difference between Code:
ps aux | grep [c]ron |
Quote:
Code:
ps aux | grep cron | grep -v grep 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' |
All times are GMT -5. The time now is 09:21 AM. |