LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 02-16-2010, 11:05 AM   #1
m4rtin
Member
 
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:

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

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 627

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
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.

Last edited by firstfire; 02-18-2010 at 05:26 AM.
 
Old 02-18-2010, 07:29 AM   #4
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
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

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.

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


Reply


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


All times are GMT -5. The time now is 07:26 PM.

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