Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-16-2010, 11:05 AM   #1
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
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:

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?
Old 02-16-2010, 11:55 AM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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" ?

Last edited by pixellany; 02-16-2010 at 11:58 AM.
Old 02-18-2010, 05:18 AM   #3
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 705

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425

If you want to compare output of two processes, try so called "process substitution" (available in BASH on some systems. see man bash):
diff <(process1) <(process2)
For example:
/tmp$ > test
/tmp$ diff <(ls -1) <(sleep 1; rm test ; ls -1)
< 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
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.

Last edited by firstfire; 02-18-2010 at 05:26 AM.
Old 02-18-2010, 07:29 AM   #4
Senior Member
Registered: May 2008
Posts: 4,433
Blog Entries: 7

Rep: Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890Reputation: 1890
Originally Posted by pixellany View Post

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

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:
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.

Last edited by GazL; 02-18-2010 at 07:39 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Requesting shell script that only outputs directories that contain subdirectories cyrilic Programming 4 01-08-2010 09:26 AM
capturing shell scripts outputs a.toraby Programming 4 11-11-2007 08:51 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
shell confusion..what is diff between bash, ksh, csh, tcsh..?? servnov Linux - Newbie 7 11-18-2004 08:28 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration