Small change to 'ps'

Bert JW Regeer xistence at 0x58.com
Wed Jan 7 18:58:49 UTC 2009


On Jan 7, 2009, at 10:27 , Sheldon Givens wrote:

> On Wed, Jan 7, 2009 at 7:48 AM, Ulrich Spoerlein  
> <uspoerlein at gmail.com>wrote:
>
>> On Wed, 07.01.2009 at 08:54:41 -0600, Sean C. Farley wrote:
>>> On Wed, 7 Jan 2009, Ulrich Spoerlein wrote:
>>>
>>>> On Tue, 06.01.2009 at 11:52:39 -0800, Sheldon Givens wrote:
>>>>> Hello everyone,
>>>>>
>>>>> It occurs to me that FreeBSD ps lacks the ability to disable  
>>>>> header.
>>>>> This seems like a really obvious feature, and I may have simply
>>>>> missed it's existence (despite my relentlessly searching the man
>>>>> page) but here is a small patch that sets the flag 'n' to disable
>>>>> header output.
>>>>
>>>> You've missed it, probably because it is non-obvious:
>>>>
>>>> % ps -p 1 -o pid,cpu
>>>> PID CPU
>>>>   1   0
>>>> % ps -p 1 -o pid= -o cpu=
>>>>   1   0
>>>> %
>>>
>>> Another way:
>>> ps | tail +2
>>
>> I'm not sure about the portability of tail +N, I seem to remember  
>> that
>> AIX doesn't support it. Therefore I'd rather use
>>
>> % ps | sed 1d
>>
>> which is way more portable.

The three solutions above all work on my OpenSolaris machine (other  
than cpu=, since the field is named pcpu). I don't currently have  
access to HP-UX or AIX to test which do and do not work.

>>
>>
>> Cheers,
>> Ulrich Spoerlein
>> --
>> It is better to remain silent and be thought a fool,
>> than to speak, and remove all doubt.
>>
>
> Hello everybody,
>
> Ulrich: I appreciate your solution, but it ends up being a pretty  
> ridiculous
> command when you start desiring 6 or 7 fields.

Yes, which is why the sed/tail will do exactly what you want. UNIX was  
built on the fact that small tools together will work together to do  
something larger, no need to move all of the functionality into one  
tool, that creates maintenance nightmares and just complicates  
otherwise simple code.

>
>
>
> Sean: Ulrich is right in saying that tail +val is unreliable when  
> coding for
> portability.
>
>
> And I guess I just feel like running a second command to do what  
> should be
> possible to do with the first command (and is, on many platforms. ps
> --no-headers on linux for example) is a problem and presents  
> opportunity for
> continued refinement of the utility.

What other platforms besides Linux distributions? Just because they  
exist by the hundreds does not mean they are different platforms.

xistence at Keyhole.network.lan:~$ ps -AflZ --no-headers
ps: illegal option -- no-headers
usage: ps [ -aAdefHlcjLPyZ ] [ -o format ] [ -t termlist ]
         [ -u userlist ] [ -U userlist ] [ -G grouplist ]
         [ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z  
zonelist ] [-h lgrplist]
   'format' is one or more of:
         user ruser group rgroup uid ruid gid rgid pid ppid pgid sid  
taskid ctid
         pri opri pcpu pmem vsz rss osz nice class time etime stime  
zone zoneid
         f s c lwp nlwp psr tty addr wchan fname comm args projid  
project pset lgrp
xistence at Keyhole.network.lan:~$ uname -a
SunOS Keyhole.network.lan 5.11 snv_101b i86pc i386 i86pc Solaris

I also read the entire man page for ps that is included and could not  
find a single way to not include a header by using a simple flag, in  
the man page they list the -o format as the only way to remove the  
header.

HP-UX man page does not include an option either, besides the  
aforementioned -o format option:

http://docs.hp.com/en/B2355-90690/ps.1.html

Same goes for IBM's AIX system:

http://publib16.boulder.ibm.com/doc_link/en_US/a_doc_lib/cmds/aixcmds4/ps.htm

Just because Linux has it does not mean it is an improvement that  
should be imported. I think it is rather dumb idea really. What if in  
the future we add a new field to the output of ps to give more  
information (on Solaris for example, the -Z function lists what Zone  
the command is running in (global, or non-global)), this changes the  
amounts of fields that are listed. Now lets say some shell script that  
was written a few years ago requires a certain option to be located in  
field 7, and now that is moved over to field 8 it could cause all  
kinds of issues, instead if the command is written out with -o pid=,  
it is static and it won't change even if a new field is introduced to  
ps, thereby decreasing future issues, and increasing portability since  
you can select only the fields you require, making it more portable  
(if using standardised fields) because now the field will always be in  
the same place, no matter the platform.

>
>
> Sheldon

Linuxisms that are bad should not be imported into FreeBSD. There is a  
valid and documented way to suppress the header information using ps,  
use it. If you are using this in a script of some sort, writing it out  
once will save you a lot of headache and trouble later.

Bert JW Regeer



More information about the freebsd-hackers mailing list