[opensuse] BASH - shorter way to isolate PID and process name from 'ps ax'?
Bash gurus, I need a sanity check on my approach to just getting the PID and process name from 'ps ax'. I have a way that works well, but I think I've done it the long way. What is the simplest way to do this? I've thought about cut and the other small string chopping routines, but I've ended up just using substring extraction and substring removal to chop the pieces out of the lines I want. Here is what I've got. There are 2 snippets below. One where I just read the 'ps ax' output, and a second where I save that information in an array just in case that ends up working out better with later tests, etc...: ## basic function to trim whitespace from each end of string ## dunno who I got this from, but it is great... trimWS() { [[ -z $1 ]] && return 1 strln="${#1}" [[ strln -lt 2 ]] && return 1 trimSTR=$1 trimSTR="${trimSTR#"${trimSTR%%[![:space:]]*}"}" # remove leading whitespace characters trimSTR="${trimSTR%"${trimSTR##*[![:space:]]}"}" # remove trailing whitespace characters echo $trimSTR return 0 } ## set the internal field separator to only break on newlines OIFS=$IFS IFS=$'\n' ## initialize variables and array to hold process information psearch="$1" let count=0 declare -a parray parray=( $(ps ax | grep -v "grep\|${0##*/}" | grep ${psearch}) ) ## read matching process names from ps ax w/o the array for i in $(ps ax | grep -v "grep\|${0##*/}" | grep ${psearch}); do ppid=$(trimWS ${i:0:5}) pname=${i#*:} pname=${pname:3} echo "[$count] - ppid: $ppid pname: $pname" (( count += 1)) done ## test the array (just in case I want it for later use) for ((j=0;j<${#parray[@]};j++)); do tmp=${parray[$j]} ppid=$(trimWS ${tmp:0:5}) pname=${tmp#*:} pname=${pname:3} echo "j: $j - ppid: $ppid pname: $pname" done Both reading directly from 'ps ax' and storing the information in an array and processing it work fine. The main intent was to handle the 1-5 digit integer PID which is what drove this approach. (though in later thought, I guess it doesn't matter. If I'm just going to call kill with the PID, I could just pass the 5 character string to it and not worry about trimming the whitespace...) All of this seems like the long way to get the PID and process from 'ps ax' output, but it is what I know. So what say the experts? Is there an easier/shorter way to skin this cat? -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
* David C. Rankin
All of this seems like the long way to get the PID and process from 'ps ax' output, but it is what I know. So what say the experts? Is there an easier/shorter way to skin this cat?
seems like a lot of work: wahoo:~> pidof mutt 25495 rpm -qf `which pidof` sysvinit-2.86-216.1.x86_64 -- Patrick Shanahan Plainfield, Indiana, USA HOG # US1244711 http://wahoo.no-ip.org Photo Album: http://wahoo.no-ip.org/gallery2 Registered Linux User #207535 @ http://counter.li.org -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Fri, 26 Nov 2010, David C. Rankin wrote: [..]
parray=( $(ps ax | grep -v "grep\|${0##*/}" | grep ${psearch}) )
Variant 1 (not available on other Unices(?), e.g. MacOS X, not sure since when it's been available on Linux (IIRC >10 yrs)): pidof "$psearch" ### uses 'killall' behind the scenes, ### which potentionally might be a ### security risk Variant 2 (portable as much as the 'ps ax' output is, when in doubt, IIRC you can further boil that down to variant 2portable): ps ax | awk -vpsearch="$psearch" '$5 ~ psearch { print $1; }' Variant 2portable? (at least a lot more AFAIK): ps -eo pid,cmd | awk -vpsearch="$psearch" '$2 ~ psearch { print $1; }' AFAIR 'ps -eo' is POSIX and as portable as you can get in regard with 'ps'. man ps / info ps has a lot to say about (non-)portability of various flags and fields for -eo and such ;) And without a subshell (helps handling variables), but bash-specific process substitution: Variant 2bashism: awk -vpsearch="$psearch" '$5 ~ psearch { print $1; }' < <(ps ax) or awk -vpsearch="$psearch" '$2 ~ psearch { print $1; }' < <(ps -eo pid,cmd) Tried & tested with "xterm" for "psearch" and 9 of those running. Using just $5 also avoids the usual problem with "grepping" for the name of a name process, where the grep may appear in the output as well, but $5 is just the name of the executable, i.e. xterm itself or awk (both with or without path, but none of it's arguments which may contain "xterm"). At least use IFS and read/set to parse columns: Variant 3: ps ax | grep '[x]term' | while read -r pid rest; do echo $pid; done (you need to massage the 'grep' argument, else you end up with grep in the result more often than not, so not easy if you want to grep for a variable, and once you do anything more than that one simple grep, you might as well just use awk already! David C.R. & ALL: if you want to do _anything_ with "fields", "colums" or some such, or simply "text" generally, forget bash, cut and alike, just use awk or perl rightaway, it saves you _tons_ of trouble. bash's internal string stuff, cut, etc. pp. _have_ their uses, but "parsing" text (in columns or not) is not it (aside from splitting fields using "IFS" and set/read). Know thy tools, eh? ;) HTH & go RTFM (again) ;P -dnh, regularly reRTFMing, reading 'man bash' for over 10 years now, regularly, and regularly discovering new stuff (with the 10year old bash I had on my old box!). Sadly though, I've finally decommissioned that old box, so now I've to rely on my memory and on the current bash/documentation on the new box (running mainly oS 11.2 for now). -- The question I have is if you take the 'bugs/spyware/IE/Media player/ other undesirables' (you were talking about 'fixing' it) out of Windows, is it going to be worth using? Would it then fit on a single density floppy? -- J. Ramsey [2004/02/13] -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David C. Rankin said the following on 11/27/2010 12:41 AM:
Bash gurus,
I need a sanity check on my approach to just getting the PID and process name from 'ps ax'.
Of what? The calling function? How about cat /proc/$$/cmdline Or substitute for "$$" whatever process ID you want. Guys, you seem to be doing this the hard way when there is an easy way. Either that or I completely misunderstand what you are asking. -- Only one absolute certainty is possible to man, namely that at any given moment the feeling which he has exists. Thomas H. Huxley -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (4)
-
Anton Aylward
-
David C. Rankin
-
David Haller
-
Patrick Shanahan