<?xml version="1.0" encoding="UTF-8"?>
<!-- **************************************************************************
.... For copyright and licensing terms, see the file named COPYING.
.... **************************************************************************
.-->
<?xml-stylesheet href="docbook-xml.css" type="text/css"?>

<refentry id="ps">

<refmeta xmlns:xi="http://www.w3.org/2001/XInclude">
<refentrytitle>ps</refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo class="manual">user commands</refmiscinfo>
<refmiscinfo class="source">nosh</refmiscinfo>
<xi:include href="version.xml" />
</refmeta>

<refnamediv>
<refname>ps</refname>
<refpurpose>process listing tool</refpurpose>
</refnamediv>

<refsynopsisdiv>
<cmdsynopsis>
<command>ps</command>
<group choice="req">
<arg choice="plain">-A</arg>
<arg choice="plain">-a -x</arg>
<arg choice="plain">-p <arg choice="plain"><replaceable>process-id(s)</replaceable></arg></arg>
</group>
<arg choice="opt">-7</arg>
<arg choice="opt">-d</arg>
<arg choice="opt">-e</arg>
<arg choice="opt">-O <arg choice="plain"><replaceable>name(s)</replaceable></arg></arg>
<arg choice="opt">-o <arg choice="plain"><replaceable>name(s)</replaceable></arg></arg>
<arg choice="opt">-f</arg>
<arg choice="opt">-j</arg>
<arg choice="opt">-l</arg>
<arg choice="opt">-u</arg>
<arg choice="opt">-w</arg>
</cmdsynopsis>
</refsynopsisdiv>

<refsection><title>Note</title>

<para>
This is generally only accessible as a subcommand of <citerefentry><refentrytitle>system-control</refentrytitle><manvolnum>1</manvolnum></citerefentry>, to eliminate conflicts with the standard command by this name.
It is referred to without qualification in this manual for simplicity.
</para>

</refsection>

<refsection><title>Description</title>

<para>
<command>ps</command> is a tool for listing process information.
It is a shim for the <citerefentry><refentrytitle>list-process-table</refentrytitle><manvolnum>1</manvolnum></citerefentry> command, that provides a more familiar interface (that of the BSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry> command) to that command, whilst yielding that command's table-format output.
Its output is therefore machine-parseable and suitable for <citerefentry><refentrytitle>console-flat-table-viewer</refentrytitle><manvolnum>1</manvolnum></citerefentry> with 1 header line and <arg choice='plain'>--format tabbed</arg>.
</para>

<para>
It has just two selection modes.
The complex selectors of the BSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry> command are not supported.
</para>
<itemizedlist>
<listitem><para>
With the <arg choice="plain">-A</arg>, or (both) the <arg choice="plain">-a</arg> and <arg choice="plain">-x</arg>, command-line options, <citerefentry><refentrytitle>list-process-table</refentrytitle><manvolnum>1</manvolnum></citerefentry> is told to display the entire process table.
</para></listitem>
<listitem><para>
With the <arg choice="plain">-p</arg> command-line option, <citerefentry><refentrytitle>list-process-table</refentrytitle><manvolnum>1</manvolnum></citerefentry> is told to display just the process table entries for <replaceable>process-id(s)</replaceable> which is a comma-separated list of process IDs.
</para></listitem>
</itemizedlist>

<para>
More complex selection and sorting can be done with a normal Unix filter.
Because the output is machine-parseable it can be reliably processed by tools such as <citerefentry><refentrytitle>awk</refentrytitle><manvolnum>1</manvolnum></citerefentry> and <citerefentry><refentrytitle>sort</refentrytitle><manvolnum>1</manvolnum></citerefentry> without the problems that would be caused by the whitespace in time, argument, environment, and tree fields.
See <citerefentry><refentrytitle>list-process-table</refentrytitle><manvolnum>1</manvolnum></citerefentry> for the output encoding details.
</para>

<para>
The <arg choice="plain">-7</arg> command-line option is transformed into the <arg choice="plain">--7-bit</arg> option.
</para>

<para>
The <arg choice="plain">-H</arg> command-line option is transformed into the <arg choice="plain">--threads</arg> option.
</para>

<para>
The <arg choice="plain">-w</arg> command-line option is present for compatibility and does nothing.
The output of <citerefentry><refentrytitle>list-process-table</refentrytitle><manvolnum>1</manvolnum></citerefentry> is designed for a table-viewer, or processing by another utility, and is not constrained by terminal width.
</para>

<para>
The <arg choice="plain">-o</arg> command-line option is transformed into a sequence of <arg choice="plain">--field</arg> options, one for each of the fields in <replaceable>name(s)</replaceable>, which is a comma-separated list.
The use of this option nullifies the effects of all other format options, including <arg choice="plain">-O</arg>.
</para>

<para>
If the <arg choice="plain">-o</arg> command-line option is not used:
</para>
<itemizedlist>
<listitem><para>
The <arg choice="plain">-f</arg>, <arg choice="plain">-j</arg>, <arg choice="plain">-l</arg>, and <arg choice="plain">-u</arg> command-line options are transformed into sequences of <arg choice="plain">--field</arg> options specifying the (hardwired) fields for "full", "jobs", "long", and "user" formats, respectively.
If none of these options are set, a "default" set of fields is employed.
</para>
<note>
These options are accumulative rather than exclusive.
If multiple options are used, all of the fields from all sets will be included.
</note></listitem>
<listitem><para>
The <arg choice="plain">-O</arg> command-line option is transformed into a sequence of <arg choice="plain">--field</arg> options, one for each of the fields in <replaceable>name(s)</replaceable>, which is a comma-separated list.
This sequence is inserted into the "default"/"full"/"jobs"/"long"/"user" list after the <code>pid</code> column.
</para>
<note>
No deduplication of fields is done, so <citerefentry><refentrytitle>list-process-table</refentrytitle><manvolnum>1</manvolnum></citerefentry> will receive any repetitions exactly as they are.
</note></listitem>
</itemizedlist>

<para>
If an output column is either <arg choice="plain">command</arg> or <arg choice="plain">args</arg>, the <arg choice="plain">-d</arg> command-line option is transformed into <arg choice="plain">--field tree</arg> preceding it and the <arg choice="plain">-e</arg> command-line option is transformed into <arg choice="plain">--field envs</arg> following it.
Otherwise they have no effect.
</para>

</refsection>

<refsection><title>Examples</title>

<informalexample>
<para>
This command uses <command>ps</command> to display the process tree for the current user, selecting rows using <citerefentry><refentrytitle>awk</refentrytitle><manvolnum>1</manvolnum></citerefentry>.
Even though the process tree diagram and argument strings contain whitespace, they are in an encoded form when <citerefentry><refentrytitle>awk</refentrytitle><manvolnum>1</manvolnum></citerefentry> processes them and there is no ambiguity about field boundaries, which are exactly 1 TAB.
</para>
<literallayout><computeroutput>$ </computeroutput><userinput>ps -A -d -o "pid,uid,args" |</userinput><computeroutput>
&#x3e; </computeroutput><userinput>awk -F'\t' -v UID="$(id -u)" \</userinput><computeroutput>
&#x3e; </computeroutput><userinput>    '($2 == UID) { printf "%8u %s %s\n",$1,$3,$4 }' |</userinput><computeroutput>
&#x3e; </computeroutput><userinput>unvis</userinput></literallayout>
</informalexample>

<informalexample>
<para>
This command uses <command>ps</command> to display all processes run by the current user whose (accounting) names are "chrome".
Again, the field matching by <citerefentry><refentrytitle>awk</refentrytitle><manvolnum>1</manvolnum></citerefentry> has no ambiguity, and does not accidentally match the wrong columns or partial strings.
(It is Chrome itself that incorrectly rewrites its argument strings into one single long argument, by the way.)
</para>
<literallayout><computeroutput>$ </computeroutput><userinput>ps -A -o "pid,uid,comm,args" |</userinput><computeroutput>
&#x3e; </computeroutput><userinput>awk -F'\t' -v UID="$(id -u)" \</userinput><computeroutput>
&#x3e; </computeroutput><userinput>    '($2 == UID &amp;&amp; "chrome" == $3) { printf "%8u %s\n",$1,$4 }' |</userinput><computeroutput>
&#x3e; </computeroutput><userinput>unvis</userinput></literallayout>
</informalexample>

<informalexample>
<para>
This command uses <command>ps</command> in a loop to feed <citerefentry><refentrytitle>console-flat-table-viewer</refentrytitle><manvolnum>1</manvolnum></citerefentry>, producing a continually updated display akin to <citerefentry><refentrytitle>top</refentrytitle><manvolnum>1</manvolnum></citerefentry>, with Form Feeds to clear the table display at each iteration.
</para>
<literallayout><computeroutput>$ </computeroutput><userinput>while ps -a -x | sed -e $'1i\\\n\\\f'</userinput><computeroutput>
&#x3e; </computeroutput><userinput>do</userinput><computeroutput>
&#x3e; </computeroutput><userinput>    sleep 1</userinput><computeroutput>
&#x3e; </computeroutput><userinput>done |</userinput><computeroutput>
&#x3e; </computeroutput><userinput>console-flat-table-viewer --header-count 1 --format tabbed</userinput></literallayout>
</informalexample>
<note>
Using <citerefentry><refentrytitle>sed</refentrytitle><manvolnum>1</manvolnum></citerefentry> to insert the Form Feed rather than printing it separately with <citerefentry><refentrytitle>printf</refentrytitle><manvolnum>1</manvolnum></citerefentry> reduces flicker.
(The standard output buffering in <citerefentry><refentrytitle>sed</refentrytitle><manvolnum>1</manvolnum></citerefentry> causes the write to be a single system call, causing <citerefentry><refentrytitle>console-flat-table-viewer</refentrytitle><manvolnum>1</manvolnum></citerefentry> to receive the Form Feed plus replacement data in a single lump, causing it not to redraw a blank intermediate screen.)
</note>

</refsection>

<refsection id="COMPATIBILITY" xreflabel="COMPATIBILITY">
<title>Compatibility</title>

<para>
This <command>ps</command> is modelled on the FreeBSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry> tool, providing a subset of its functionality; with the <arg choice="plain">-A</arg> and <arg choice="plain">-f</arg> command-line options taken from the the Single Unix Specification, and the <arg choice="plain">-O</arg> command-line option taken from the NetBSD and OpenBSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
It provides the <emphasis>actual</emphasis> command-line option syntax that the BSDs have employed and documented for 29 years, at the time of writing this manual.
</para>

<para>
It differs from the Single Unix Specification in the same ways that the BSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry> differs from the Single Unix Specification, namely in the <arg choice="plain">-d</arg>, <arg choice="plain">-e</arg>, <arg choice="plain">-g</arg>, <arg choice="plain">-n</arg>, and <arg choice="plain">-u</arg> command-line options.
There are three exceptions:
</para>
<itemizedlist>
<listitem><para>
It implements the Single Unix Specification´s <arg choice="plain">-A</arg> and <arg choice="plain">-f</arg> command-line options.
The former is already in most of the BSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry> commands, except for FreeBSD's.
The latter has a widely employed standard meaning and only has another meaning at all in the FreeBSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry> command.
</para></listitem>
<listitem><para>
It uses the Single Unix Specification's <code>stime</code> instead of the FreeBSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry>'s <code>start</code>.
</para></listitem>
<listitem><para>
It does not implement the <arg choice="plain">-G</arg>, <arg choice="plain">-t</arg>, and <arg choice="plain">-U</arg> command-line options, which are implemented by BSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry> and specified in the Single Unix Specification.
</para></listitem>
</itemizedlist>

<refsection id="HISTORY" xreflabel="HISTORY">
<title>History</title>

<para>
The <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry> tool from the procps toolset (as sometimes used on Linux operating systems) has an exceedingly complex command-line interface.
One of the claims made in its documentation is that this complex command-line interface provides the command-line interface of the BSD <command>ps</command>, where options are not preceded by minus signs.
Michael K. Johnson's conversion from "kmem-ps" to "procps" of the Linux <command>ps</command> command as modified by Albert D. Cahalan originated the claim that that <command>ps</command> command was providing an "extended BSD" command-line interface, a claim that has stood ever since.
</para>

<para>
This is not, and was not, true at all.
</para>

<para>
In fact, the BSD <command>ps</command> has not worked that way since the days of 4.3BSD in the 1980s, <ulink url="https://svnweb.freebsd.org/csrg/bin/ps/ps.c?r1=40257&amp;r2=40675">Marc Teitelbaum having switched it to using the <citerefentry><refentrytitle>getopt</refentrytitle><manvolnum>3</manvolnum></citerefentry> library function on 1990-04-02</ulink>.
It had, in other words, not worked that way for 2 years prior to the creation of Branko Lankester's <emphasis>original</emphasis> Linux "kmem-ps" <command>ps</command>, in 1992.
</para>

<para>
The BSD (and later FreeBSD and NetBSD) <command>ps</command> has used <citerefentry><refentrytitle>getopt</refentrytitle><manvolnum>3</manvolnum></citerefentry> ever since.
This has been the only documented command-line syntax in its manual for all of that time, too.
<emphasis>This decades-long-standing BSD command syntax</emphasis> is the model here.
</para>

<para>
The <arg choice="plain">-O</arg> command-line option to the BSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry> tool originally operated somewhat differently, adding the fields specified in the option argument <emphasis>plus</emphasis> the "default" set of fields to the desired set of fields.
This <ulink url="http://cvsweb.netbsd.org/bsdweb.cgi/src/bin/ps/ps.c?rev=1.61&amp;content-type=text/x-cvsweb-markup">was deemed not particularly useful by NetBSD developers in 2006</ulink>, as it resulted in a lot of pointless column duplication, and was revised.
This revised behaviour made its way from NetBSD into the OpenBSD <citerefentry><refentrytitle>ps</refentrytitle><manvolnum>8</manvolnum></citerefentry>.
It has not yet made it into the FreeBSD flavour of the tool as of the time of writing this manual, hence the aforegiven ascription of <arg choice="plain">-O</arg> to NetBSD and OpenBSD, rather than to the BSDs in general.
</para>

</refsection>

</refsection>

<refsection><title>Author</title>
<para><author><personname><firstname>Jonathan</firstname> <surname>de Boyne Pollard</surname></personname></author></para>
</refsection>

</refentry>
