JP Vossen on 29 Apr 2008 09:04:51 -0700

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] ps portability (was: my netstat must be broken ...)

 > Date: Tue, 29 Apr 2008 08:52:36 -0400
 > From: "Paul L. Snyder" <>
 > GNU ps has multiple personality disorder but means to be helpful, so
 > it attempts to infer what you want based on the style of command-line
 > options you are using, so your output may differ simply because you
 > used or did not use a dash and ps is emulating BSD or SysV default
 > behavior.
 > I find that differing versions of 'ps' are one of the most annoying
 > things to adapt to when bouncing between different flavors of Unix.
 > Solaris 'ps' drives me nuts.

YES!!!  'ps' is one of the bigger pains when trying to write portable 
shell scripts.  GNU ps tries to be helpful, and it is, if you squint a 
bit, but as Paul said, unless you understand what is happening it can 
drive you crazy.  Actually, even when you do understand it, it can drive 
you crazy.

Sending email from the command line is another one, though it's not as 
bad as 'ps'.  I often try to cheat and have cron to do my emailing for 
me when I can.

Here is some code I wrote for a fancy init.d script that has to run on 
Linux, AIX, and Solaris (thus far, I threw in the others for the heck of 
it).  Part of the challenge is that this script it controls is often 
multi-instance, differentiated only by the associated config file.  And 
the paths get long, so 80 characters is often not long enough to fit the 
config file name.  What fun.  Not.

----- begin shell code -----
# Can you believe this?!?
case `uname` in
     # Solaris is a problem, as its ps is hard-coded to limit 'args' to
     # use only 80 characters.  If there are long paths to Perl,
     # ... will fail with a 'PID mis-match' error.  We can use the UCB
     # version of ps instead, but it does not support the 'o' option!
     # Sigh.
     Linux)     PS='ps -ewwo pid,args'
                MAILER='mail'  ;;     # Red Hat has no mailx by default
     AIX)       PS='ps -eo pid,args'
                MAILER='mail'  ;;
     SunOS)     PS='/usr/ucb/ps -axww | cut -c1-7,25-'
                MAILER='mailx'  ;;    # mail -s doesn't work on Solaris
     *BSD)      PS='ps awwo pid,args'
                MAILER='mail'  ;;
     Darwin)    PS='ps Awwo pid,command'
                MAILER='mail'  ;;
----- end shell code -----

JP Vossen, CISSP            |:::======|        jp{at}jpsdomain{dot}org
My Account, My Opinions     |=========|
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.
Philadelphia Linux Users Group         --
Announcements -
General Discussion  --