ps command on Linux (view processes)
The <span style=“font-size: revert; color: initial; font-family: -apple-system, BlinkMacSystemFont, “Segoe UI”, Roboto, Oxygen-Sans, Ubuntu, Cantarell, “Helvetica Neue”, sans-serif;“>ps command in Linux generates a list of all running processes and their attributes.
There was a time in Linux when two versions of the ps command coexisted in different distributions. Not long ago, the developers decided to combine the two versions of PS into a single version, but left the options of both different versions coexisting.
Thus, when entering a parameter in the PS, the results will be different:
- Without using the minus sign ”-”, ps behaves by showing processes in the BSD style;
- with a minus sign just “-” the ps behaves showing processes in the Unix style, using the POSIX standard;
- With two minus signs “—” ps behaves by showing processes in the GNU style.
There is no right or wrong, just historical preference.
The most common options for ps are:
- a Shows the running processes connected to a terminal, of all users;
- -a Shows the running processes connected to a terminal, minus the session processes;
- -e, -A Shows all processes;
- -u Shows the list of processes including the name of the users who own the processes and start of executions, percentage of CPU used, percentage of memory used, and associated terminal;
- -x Shows the list of processes, including those that they don’t have a terminal associated with it. Useful for viewing server processes (daemons);
- -f Shows processes in the form of a tree. Very useful for identifying the parent-child process relationship between running processes;
- -H Shows the hierarchy of processes in the form of a tree;
See the ps command help for more options.
In this example, PS only shows the processes of the user logged in and connected to the terminal:
To show all processes for all users connected to a terminal:
Notice how the “-a” option is different from the “a”:
The “u” option adds some process attributes:
To get a complete list of running processes, not just those that are connected to the terminal, add the “x” option:
Processes whose commands are wrapped in keys, such as in the [ktheradd] highlight, indicate that they were removed from RAM and placed in virtual disk memory. When processes are in virtual disk memory, they are referred to as sleeping.
The Kernel often puts processes to sleep while they are waiting for an event, which may be, for example, loading data from the disk, or a network connection. When the event is triggered, the Kernel sends a signal to the process.
The “eFH” options show all processes, with their hierarchy in the form of a tree:
It is possible to play with the commands, such as, for example, knowing which processes consume the most CPU:
A very interesting option for programmers is to see the processes running ordered by memory consumption. To do this, simply change the sort column to 4:
Regardless of how you want to view the processes running, some attributes are important for a Linux administrator, such as:
Process Owner User (UID)
It’s impossible to run a program on Linux without it having an owner user. This means that the program will have the disk access permissions and resources of the user who ran the program. This notion is important because it is possible to execute a program as another user other than the logged-in user.
Process Number - Process ID (PID)
Every running program is assigned a unique numeric ID. This number can be used to send signals to the running program.
Parent Process (PPID)
Every program except init or systemd has a parent process that originated its execution. It is common for a server program, for example, to have a parent process (master) and several processes or threads (children).
Threads can be understood as pieces of the program that execute as if they were child processes, but are lighter, since they share a lot with the parent process. This technology is widely used in processors with several cores (cores) to allow several threads to run at the same time.
% of CPU
Each process gains a share of CPU time, which can be counted, and serves as a parameter for the administrator to know which processes consume a lot of CPU.
Memory%
Each running process also gains a share of RAM, which can also be counted to let the administrator know the processes that consume a lot of RAM
Start time (STIME)
Each process is also attributed to the time at which it was executed.
CPU time (TIME)
Each process also has as its attribute the CPU time accumulated during its execution.
Command line (CMD)
The processes also maintain an attribute, which is the command line that was used in the execution.
Terminal (TTY)
Each process may or may not have an associated Terminal. As a curiosity, what the ps command actually does is to scan some information from the /proc directory. Take the sshd process as an example:
If we look at the /proc directory, it will have a subdirectory with the number 3252, which corresponds to the PID of the sshd program:
com dinfo maps numa_maps project_map smaps_rollup timers
For example, the program’s command line with PID 3252 can be consulted with a simple cat:
Various information about a process can be viewed in the /proc/ directory [PID number]. The ps utility only organizes the information for a more humane reading.