- [Instructor] A Linux system may have hundreds of processes
running at any time.
The first tool we'll look at to monitor processes
is the ps command.
In a terminal, type in ps and hit Enter.
By default, ps only shows the processes
run by the user executing it.
It shows the process ID,
the terminal that was run on,
the aggregated execution time,
and the command that was run.
Oddly, the ps command has three different types
of syntax options: Unix, BSD, and GNU.
The Unix options looks as you'd expect
with a dash before a single letter.
The GNU options are words with two dashes
just like other commands.
The odd ones are the BSD options
which don't have any dashes at all.
For this course,
we'll focus on the Unix and GNU options.
To get ps to display every process,
we'll want to use the -e option.
Type in "ps -e" and hit Enter.
This still shows the same columns as ps with no options,
but shows every process.
To give us an idea of
which processes start at other processes,
we can pass the Hierarchy option, or -H.
Type in "ps -eH".
We can see with indentation
which processes are child processes.
You can add this option to any others
that we'll cover in this course.
To show more information about all processes,
add the -f or Full option.
Type in "ps -ef" and hit Enter.
This adds the user name to run the command,
the parent process ID, CPU usage, the start time,
and the command with arguments and options.
This is usually my go-to set of options for ps.
If you want more information we can change the lowercase F
to an uppercase F.
Type in "ps -eF" and hit Enter.
This adds allocated memory, the total memory size,
and the CPU the process is running on.
If that's not enough,
you may want to add the -l for long format.
Type in "ps -elF" and hit Enter.
This gives us 17 columns of information.
Check the man page for more information
on each of these columns.
A lot of the information in the ps -elF I don't need
on a regular basis.
You can customize what are shown on the screen
by passing the --format long option.
For instance, type, "ps -e --format uid,pid,ppid,%cpu,cmd"
and hit Enter.
This shows the user, the process ID,
the parent process ID, the CPU utilization,
and the command for all processes.
We can also sort on any field.
Bring your line back by pressing the upper arrow key,
and append "--sort %cpu" and hit Enter.
This will allow us to show all processes
and sort by CPU usage from least to greatest.
If you want to sort from greatest to least,
bring your line back and place a hyphen before the %cpu
and hit Enter.
We can also show processes just for one user or group.
Let's show all processes run by the root user.
Type in "ps -U root" and hit Enter.
Using the uppercase U, we can specify the user's name.
If you want to use the user's numeric ID,
use a lowercase U.
We can specify the group name with an uppercase G,
and a group ID with a lowercase G.
We can also specify the program name as well.
For instance, if we had multiple copies of a program running
we could show just those processes
by specifying the -C option.
To set this up, I'm going to run
a couple Firefox web browsers.
Go to Applications, Favorites, Firefox.
I'll go ahead and start a second one
by going to Applications, Favorites,
Firefox web browser again,
and then I'll go back to my terminal.
Now type in "ps -C firefox" and hit Enter.
This becomes more useful when we specify --format
and --sort.
Type in
"ps -C firefox --format pid,uid,%cpu,cmd --sort %cpu"
and hit Enter.
This shows the CPU usage for the Firefox process.
There's a dizzying array of options for ps.
Here's a couple of my favorite ps lines.
Type in "ps -e --format uid,pid,tty,%cpu,cmd --sort %cpu"
and hit Enter.
This shows every process, the user ID,
the process ID, the terminal they were run on,
the CPU usage, and the command name
sorted by highest CPU usage.
Another one I like is
"ps -e --format uid,pid,tty,rss,cmd --sort rss"
and hit Enter.
This shows the user, process ID, terminal, normal usage,
and the commands sorted by memory usage.
Here's one more that I like.
It's a long one, but you can alias it if you wish.
Type in "ps -U " your username, I'll type in,
"grant --format %mem | awk '{memory +=$1};
"END {print memory}'" and hit Enter.
Enter your own username here instead of mine.
This calculates all memory usage
for this one user using Awk.
There are many, many other options for ps.
You can specify processes by user,
groups, terminals, sessions, process ID list,
parent process ID list, command list, and so on.
You can format the output any way you want
and you can gather more process details
than you'll probably ever need.
Please check the man page for more options
by typing in, "man ps".