- [Instructor] It's common to send signals to processes.
One of those signals would be terminate,
if you wanted to end the process.
If that doesn't work, we can forcibly kill a process
by sending a kill signal.
First, though, we need to know the process ID.
We've already talked about the PS command for this,
but we have a couple of other tools that are simpler.
In the terminal, type in pidof, space, crond,
and hit Enter.
Also type in pgrep, space, crond, and hit Enter.
Pgrep is more powerful and allows us to filter
on a lot of items like username and command.
But for the purpose of just getting a process ID,
either will work fine.
Once we have the process ID,
we can use the kill command to send it a signal.
To list all possible signal types,
type in kill, space, -l, and hit enter.
There are 64 different signals,
but it's not common practice to use all of them.
Generally, we want to send the process
a non-destructive signal, like SIGHUB or SIGUSR1,
or we want to terminate a process with SIGTERM or SIGKILL.
In many cases, SIGHUB forces a process
to reread its configuration file.
Read the man pages for the process in question
to see more details.
Now, for this exercise, let's start Firefox
so we can terminate it from the command line.
Go to Applications > Favorites > Firefox,
and minimize it.
Now, on the terminal, type in pidof firefox, and hit Enter.
This should return the process ID in Firefox.
Now that we have the process ID,
we can send it a TERM signal.
Type in kill, space, and then the process ID.
In my case, it's 4631,
but in your case, it's probably different.
And hit Enter.
If we don't specify a signal, the default number is 15,
a SIG term.
Notice that Firefox is no longer listed
in our application switcher at the bottom of the screen.
Verify this with pidof to show that it's gone.
Type in pidof, space, firefox, and hit Enter.
Sometimes, we don't just want to send a terminate signal
but rather want to get the process's attention.
Different programs use signals differently,
so check the manual pages for each.
One example is the DD command.
DD is used to duplicate disks, but has no visual output.
Sending it a signal will force it
to show output on the screen.
To set this up, let's open another terminal
by going to File > Open Terminal.
I'm going to drag that terminal to the right side
of the screen, and we're going to bump the font size.
We'll double-click the title bar of our original terminal,
and we'll drag that one to the left screen.
And type in clear to clear the screen.
Now, we should be able to see both of our terminals.
In the right-hand terminal,
type in dd if=/dev/zero, space,
of=/dev/null, and hit Enter.
This really does nothing but read zeroes from dev/zero
and output them to dev/null where they're discarded.
But for our purpose, we can see how to make
DD output progress using signals.
Notice in the right-hand terminal, there's nothing
on the screen in the way of visual progress.
Now, let's go to our left terminal
and send it a USR1 signal.
The USR1 signal is shorthand for SIGUSR1,
or signal number 10.
Let's get the process ID of the DD command
by typing in pidof dd, and hit Enter.
Now, we're going to take that number
and send the USR1 signal to it.
Type in kill, space, -USR1, space,
and then the process ID from the previous command.
In my case, it's 4868, but in your case,
it's probably going to be something different.
And hit Enter.
Notice that the DD process in the right-hand window
gave us a progress update.
The two-step process that we did can be one step
using command substitution
that we covered in chapter one of this course.
Type in kill, space, -USR1, space,
$(pidof dd), and hit Enter.
This gets the process ID of DD
and then passes it to the kill command.
Now, let's kill the DD process, as we don't need it anymore.
Type in kill, space, $(pidof dd), and hit Enter.
We don't need the second terminal anymore,
so close it, and resize the left one.
The kill command always needs the process ID.
If, however, we want to kill all processes
with a certain name, we can use the killall command.
Let's say that we wanted to kill all crond processes.
We'd type in sudo, space, killall,
space, crond, and hit Enter.
Type in your password and hit Enter again.
We need to elevate privileges,
because crond is a system service.
It won't really hurt anything to kill it,
but we will want to start it back up again in a moment.
Just like kill, the default signal
for killall is SIGTERM or signal 15.
If you need to send all commands or a certain name
a different signal, just specify it.
Before we move on, let's start crond back up.
Type in sudo, space, systemctl, space start,
space crond, and hit Enter.
Verify this with pidof, space, crond.
Well, it should show you the process ID number of crond.
One last thing I want to cover
is killing all processes for a user.
There is a real-world case where you need to stop
all processes belonging to one user.
You might need to do this because you're remounting
the /home directory, or you're locking a user's account.
Don't hit Enter after this line is typed.
So type in sudo, space, killall,
space, -you, space,
and your username.
Now, don't hit Enter yet.
This will end all processes run by the user grant.
Now, I'm going to hit Enter to show you what happens.
You don't have to do this,
as it will close your terminal and GUI login.