In this video, we’ll take a look at monitoring and managing processes on Oracle Linux.
A process is a running instance of a program.
Programs are executable files, which are passive until the program launches, at which time
it becomes a process.
So every time you run a command in the shell it executes a program and starts a new a process.
Processes are sometimes referred to as tasks, so you might see these terms used interchangeably.
At any given time there will be multiple processes running on the system.
Each process is assigned a unique number, or PID, which we can use to monitor and manage
the process.
Let’s jump into a terminal and take a look.
One of the ways we can monitor processes is using the ps command.
Running just ps, shows us active processes for this shell.
For instance, we see that the ps process has a PID of 2800.
If we wanted to see all processes running on the system, we can use the e option and
then use the f option to see more columns of information.
So let’s try running ps space –ef.
If we scroll up, we can see what the columns are.
There's the user associated with the process, the process id, parent process id, CPU usage,
start time, controlling terminal, process time, and finally the command with options
and arguments.
We discussed how every process has a unique ID, which we see here, but what’s the PPID?
To complete a function, some processes launch other processes, which are called children
processes.
So this PPID column lists the ID of the parent process that launched it.
Let's scroll back down and look at the ps command.
It has a parent id of 2699, which is the bash shell.
If we want a more intuitive view of how the parent and children processes relate, we can
use ps –eH.
This shows a process hierarchy, with the parent processes at the top and it’s children processes
indented underneath.
If I scroll up, we see that systemd has spawned all of these underneath it.
You may have noticed that the information ps gives is just a snapshot at the time that
we run the command.
To get real-time process information, we can instead use a monitoring program.
One example is top.
Type top into the terminal and hit enter.
We see this list of processes updating every couple of seconds.
So this is giving us live updates on the processes running on the system.
At the top there’s a ton of information about the system as well.
Such as the number of processes running, total % of CPU usage, memory usage, and so on.
Top has a bunch of keyboard shortcuts and settings that you can adjust to customize
this display.
I won’t go into them here, but I encourage you to take a look at the man pages for top
to learn more.
To quit, type q.
Now that we've seen how to monitor processes, let’s take a look at managing them.
There may be times that you need to send a signal to a process.
Maybe you need to forcibly end a process that won’t respond.
Of course, you’ll want to be careful here.
You don’t want to accidentally end a process that’s going to bring the whole system down.
So for this example, I’m going to go to the desktop and open a couple applications
that we can safely terminate.
I’ll open a firefox browser and a text editor.
Then we’ll look at ending them.
In order to send a terminate signal, we’ll need to know the process ID.
We could get this from ps or top, but we can also use pidof or pgrep.
So I’ll use pidof and the text editor I’m using here is called gedit.
This shows a PID of 6199.
So to end the process, I’ll use kill and the process ID.
Your ID is going to be different than mine, so be sure to use whatever ID applies to your
system.
And if I hit enter we see the editor disappears.
If we run pidof gedit, it returns nothing.
It’s important to know that despite it’s name, kill is actually a way to send a variety
of signals to a process.
If we use kill –l, it shows us a list of all available signals.
If we don’t specify anything, like in the previous example, it sends SIGTERM which terminates
the process.
To send a different signal, we’d use kill - .
Let’s look at sending a signal to firefox.
First well need to find the PID, so pidof firefox.
But unlike the last example though, here we see there are several IDs.
That’s because firefox has several children processes running.
We’ll want to use the ID of the parent process, which is going to be the lowest value, so
this one here.
But if we don’t want to deal with thinking about that, we could instead use pgrep firefox
which shows a single id.
Pgrep is similar to pidof, but much more powerful so I encourage you to explore more on your
own.
Let’s try specifying a stop signal, so kill –SIGSTOP and the PID.
This stops the process and we see we can’t do anything within the firefox window.
We can start it up again with SIGCONT.
So kill, dash, and I can actually leave off SIG, so this time I'll just use CONT and the
PID.
And we can see the browser is running again.
Let’s try sending SIGKILL.
Normally, you’d only want to do this as a last resort when the process isn’t responding
to the standard terminate signal.
That's because the SIGKILL command is something the process can't ignore.
It forces the process to stop abruptly.
Whereas the terminate signal gives the process the option to close out nicely.
So again, you’d only want to send SIGKILL if the process isn’t responding to other
signals.
In this case, the browser is responding to us, so technically we don't need to do this,
but, for demo purposes let’s try it.
So kill, space, dash, and this time I’ll show you a little bit different syntax.
Instead of writing out SIGKILL, or KILL, we can use the numeric value for the signal,
which we see from the list above is 9.
And also I want to show you how to use variable substitution for the PID, instead of typing
the number, let's use $(pgrep firefox).
This is saying take the value we'd get back from the pgrep firefox command and use it
here.
So instead of running pgrep separately, and having to remember the PID, we can run just
one command.
If I hit enter, we see the firefox window ends and if we run pidof firefox we get no
results.
Another important thing to understand about processes, is that they can run either in
the foreground or background.
Most of the programs we run from the shell, run in the foreground.
Meaning the shell kicks off a process and has to wait until the process completes.
For instance if I run sleep 5, I can’t do anything else in the terminal.
I have to sit and wait until it completes, which in this case is after 5 seconds.
That’s because this process is running in the foreground.
To illustrate it further, let’s launch firefox from the terminal by typing firefox and hitting
enter.
It opens up a browser window, but you’ll notice, I can’t do anything in the terminal.
That’s because I’ve opened firefox up as a foreground process, so the shell is waiting
until the process finishes.
Which means to use the terminal, I’d have to close the browser.
If I wanted to bounce between the two, I’d be out of luck.
I could use ctrl+c in the terminal and get my prompt back but that will close out firefox.
Instead let’s pause firefox by using ctrl+z.
The shell shows us it’s stopped.
And if I try to do anything in firefox, it’s frozen just like when we sent SIGSTOP.
We can restart firefox in the background with bg % and the job value which in this case
is 1.
Now firefox is running as a background process meaning the shell isn’t waiting on it.
So I can use both the browser and the terminal.
To launch a program directly into the background from the shell we can add an & after the command.
Let’s try it with gedit &.
And we see the text editor opens and I’m able to use both the editor and the terminal.
We can then use the jobs command to see any background processes that were started by
this shell.
We see both gedit and firefox listed as running.
Now I’ve used GUI applications as examples here, but of course, you can run background
processes for tasks done in the terminal as well.
I'll close these out.
And now if we run jobs, we see it lists that the status as done.
I think that covers it for an introduction to processes.
I hope you have a better sense of how to monitor and manage them in Linux.
Thanks for watching!