I'm writing a short script to check if a continuous port is running on a server.
I'm using "ps -ef | grep -v grep | grep processName" and I was wondering if it was better/more reliable to just check the
return code from the command or if its better to pipe to 'wc -l' and check if it is '1' or not?
I was just curious if one method was more reliable then the other?
Looks like the UNIX server (which is probably older then myself, or pretty darn close) doesn't have the pgrep command.
It only has: grep, egrep, fgrep and zgrep...
ls -l /usr/bin | grep "grep"
-r-xr-xr-x 3 bin bin 34052 Jan 7 2011 egrep
-r-xr-xr-x 3 bin bin 34052 Jan 7 2011 fgrep
-r-xr-xr-x 3 bin bin 34052 Jan 7 2011 grep
lrwxrwxrwx 1 root system 27 May 12 2012 zgrep -> /usr/opt/freeware/bin/zgrep
Oh well, thanks anyways... I'll probably be able to get some use out of that on a slightly newer server. I guess I'll just go with the original idea.
I was just curious if one method was more reliable then the other?
AFAIK, the two methods are exactly the same reliability. If grep finds a line, $? will be zero AND there will be output so wc -l result will be at least one. I would use the $? method, because one less step, unless there is some issue about the number of lines that grep might find.
It's fine to use grep -v grep | grep pattern
The other methods suggested (pgrep and [p]attern)are fine too. But I don't think they are "better". The other methods are (a little) shorter, so that's a small advantage. But one method is subject to failure if pgrep not present, the other admittedly clever method is not obvious to many users why it works, can be confusing to many users. I would normally use the simple way you presented.
hanson44, I don't have any concerns if you think that using grep -v grep is cleaner, not that much of a waste.
But I would like to remind you that the name grep comes from the ed command: g/re/p (global / regular expression / print)
So if using regular expression: [p]rocessName clearly eliminates the need for using grep -v grep and thus save a process, then I strongly believe we should use it.
By the way it is your privilege to waste processes if you want. So feel free...
Most ps-grep pipelines are underspecified (including all instances in this thread). Consider the following:
The pattern is evidently intended to match only a command name, but then why is it allowed to match any portion of a full listing? With such a promiscuous approach, usernames, command line arguments, and ps headers can trigger a false positive.
Even if the match is restricted to the command name, if the pattern isn't anchored at both ends, substring matches could cause false positives.
These have the added limitation of not being capable of searching for a grep command.
If I were going to use ps to portably test for the existence of a process whose command name is exactly cmdName, it would look something like this:
ps -eo comm= | grep -qFx cmdName
If portability isn't a concern, on a Linux system one could also use:
ps -C cmdName >/dev/null
It's not just about being concise; pgrep makes it simple to do the right thing. Its default behavior is to match against the command name only (args are excluded unless -f is provided). Further, a pgrep instance also excludes itself from the list of results, making it trivial to search for other instances of pgrep.
While it may not be available on the OP's system, pgrep has been widely available for about 5 years now on Solaris, HP-UX, Linux, OpenBSD, NetBSD, FreeBSD, OS X, and others.
Well, I read what you said, and took a look at the man page, and I change my opinion. pgrep is "better". I just didn't realize pgrep had so many useful options. Thanks for correcting my wrong idea.
Hey Guys, thanks for the replies!! Didn't think this would spark as much as it did, but I'm glad it did! LOTS of good info here...
Yea, I wish the system I'm running this on had pgrep, I checked out the manpage for it on "MY" own laptop (OpenSuSE 11.4) and it seems like
it would have been a better option if it were available on the AIX box... But oh well I'll just have to stay with the existing command. Thanks!
Yoda,
Yea I found out about using the [..] method a while back and it worked like a charm for MANY MANY other scripts I wrote, so I might try to
include that in my current script and remove the "grep -v grep".
Thanks EVERYBODY for all your replies, much appreciated!!