Hello again, and now, in this lesson, we
are going to cover process management.
What is a process management?
Anything that has to do with the process, how
we manage it, how we start it, how we
stop it, and how we bring it in the
foreground, how we bring it in the background,
all that stuff is process management.
Let's go over all the commands
that are associated with managing processes,
the first one, are the set of commands that
we have, for putting a process in the background.
So why do we need to put a process in the background?
Let's talk about that first.
When you run a process on your terminal, meaning when you
open up a console or when you open up a PuTTY
terminal, when you run a process on your terminal, it will
run it and it will not give you your prompt back.
So if you want to do certain other tasks or you
want to exit out of it, it will stop your process.
So how do you run it in the background?
So the command is Ctrl-z.
First you run the process, then type
Ctrl-z, it will stop the process,
then you type jobs to list what is being stopped,
and then you type bg, which is
to put the process in the background.
Then we have Foreground.
So if you are done with all other tasks that
you're doing on your terminal and you want to bring
the process that you had put in the background initially
and you wanted to see the process of it, then
you type fg for Foreground, and that process will become
live again on your console.
To run a process even after you exit, meaning
if you exit or if you close your terminal
while your process is running, what will happen?
It will stop the process.
So how do you prevent it?
There's a command called nohup.
It means don't send any kind of signal to stop it.
So nohup, the process, and followed by the
sign ampersand or and sign, which is on top
of your number seven key on your keyboard.
Another command for that is nohup, the
process, and redirect sign, /dev/null, and two,
redirect, ampersand, one, and ampersand
again. Why do we have all that stuff there?
Because certain times when you run certain commands,
like nohup, it gives some kind of
warning, some kind of information about that process.
So you don't want to see that.
You want all that garbage to go out to /dev/null. So
/dev/null is kind of an output to a
door that you don't want to see the output for.
Then to kill a process by name,
you guys already know how to kill
a process by process ID, right?
So you kill, followed by the
process ID kills the process.
What if you want to kill a process by its name?
So let's say if you have a process called top, right?
So you run the command top.
If you want to kill it by the name, you
just simply type pkill, followed by the name of
the process which is top. Process priority nice,
there is a command called nice.
And why do we use it?
Because if we want to prioritize
by different level of process.
Meaning if a process is very important then the other, it
has to be prioritized right. So the niceness scale goes from
-20 to 19. The lower the number the more priority that
task gets. Now why do we really need to go with
the priority of the task, doesn't that processor CPU takes care
of that, right, it does take care of that.
But remember one thing. If you have one
CPU in your computer or laptop, it means
it will process one program at a time.
So think of it as a juggling.
So if you have two hands and you have five
balls to throw it in the air, what will happen?
Can you hold all five balls in your hands? No, right.
So think of balls as programs that you need to process.
So you would have to juggle it one after the other.
So what nice does is, nice tells hey, this ball or
this process is important to me, so make sure this stays
in the hand most often than any other balls.
So that's what nice does.
We'll talk about that when we open up
our terminal session. Then how to monitor a process is
by running the command top and how to list all
the processes that you have running in your computer,
the command is ps, followed by of course
many different options that you could use.
So anyway, let's get into our Linux machine.
I have it open right here, our console.
So I am logged in as myself.
I want to become root.
So become root.
Now I want to log into the PuTTY section as well,
so this way I could run certain different programs from one
terminal and kill it on the other terminal. So for that
to log into the PuTTY, what do I need?
I need an IP address of my machine.
The command is ip addr, or ifconfig whichever you prefer.
But if you're running latest version of Linux, then
you probably not going to have ifconfig, so run
ip command, and the IP address you'll see right
here it says 192.168.56.101.
So go into your PuTTY terminal.
I have my PuTTY terminal right here, and I'm going to type
it in my IP address which is 192.168.56.101.
Hit enter, login as root.
The first one is Ctrl-z.
So I'm going to run a command called sleep.
sleep is a command which will delay certain programs.
Let's say if you're running a program ABC, and you wanted
to run a second set of program, but you wanted to
wait for 10 seconds, so you put in a sleep command.
That's what it does, it just waits for 10 seconds,
and it goes to the next set of instructions.
So let's say if I type sleep 5, it will wait
for 5 seconds, until I got my prompt back.
You'll see, I got the prompt
back right away after 5 seconds.
So that's what sleep does.
Don't focus too much on sleep, focus on what
I'm going to teach you about process management.
So I'm going to leverage this
lesson on the command sleep,
so this way it will be easier to understand.
So if I run the command sleep 100, what will happen?
I will have to wait 100 seconds
until I get my prompt back, right?
So I want to run that process in the background.
So I will do Ctrl-z, it has stopped the process.
Now it's not running actually in the background.
To run that in the background, you have to
type the command bg, which is for background.
But let's say if you are running multiple process, and
all of them stop, some of them are running.
So the command to check what other
processes that are stopped right now,
so you just type command jobs, and you
will see there is one process that's been
stopped by the command Ctrl-z.
If you want to run that process, bring
it and send it to the background,
then you type bg.
This will send this process in the background.
See, it is running now.
So when you type jobs, you'll
see right here, it's not stopped.
See, it's running, but it is running in the background.
You could also check by ps -ef command, by typing sleep.
And you'll see right here, sleep 100.
The other sleep 60,
you could ignore that, this runs all the time
if you're running virtual machine.
Anyway, sleep 100 is still running.
What if you want to bring it
in the foreground, then you type fg.
See, it's brought back that process in the foreground.
And now you notice I do not have my prompt back. Why?
Because the process is still running.
So what if I wanted to put it back in the background?
Okay, so 100 seconds are done.
I got my prompt back.
If I wanted to put it back in
the background, I would have type Ctrl-z. Okay?
Now, the next one we have is about nohup process.
What is a nohup.
Well, nohup is if you run a
process and then you close your terminal.
Meaning this terminal, if I close it right now, let's
say if I'm running a command sleep 100, it is
going to run, or it's going to wait for 100.
Now, if I go to my console, clear the screen
and I type ps -ef and do grep sleep,
you'll see the sleep 100 is running, right?
So if I wanted to,
[No audio]
have this terminal, let's close
this terminal, I have a couple of terminal open.
So if I close this and then do ps -ef sleep,
now you'll see both sleep 100 are gone.
So every time you run a process,
it attaches itself with that terminal.
Now, how do we run a process without
interrupting that process when we close a terminal?
So, let's go to our PuTTY session
again, to run a process in the background and when you
close the terminal, it won't impact it, you run nohup,
[No audio]
and name of the process, which is sleep 100,
[No audio]
let's put a sleep 75 just to differentiate it, and ampersand.
So you hit Enter, and you will see,
now it's running that process in the background.
So when you do jobs and you'll see
it is running, but it is not impacting
your terminal, it's not attached to your terminal.
So even at this time, if you
close it, this process will still run.
Now, what about this, nohup:
ignoring input and appending output to 'nohup.out'.
Well, every time you run a nohup command,
it actually creates a file called nohup.out.
So what it has, it has all the
information, all the things that you have typed
in there, it will just record everything.
But if you just don't want to see that message comes
up on the screen, or many times when you run certain
commands, it gives you a message that this command was created
on this day, and so many other stuff.
So to get rid of that message, you could
type nohup sleep 73 just to differentiate again,
greater than sign, /dev/null, two,
greater than sign, ampersand, one, ampersand again, hit enter.
And now you will see, you did not get
all that stuff that you were getting it before.
Because what we are saying is whatever that comes up
on the screen, those error messages, a warning, or those
even informational messages, just send it to /dev/null,
don't send it on the screen.
So send it in the back.
Now, what if you want to
see what's running in the back?
We run jobs and you'll see this
one process is running in the background.
Now, again, what if you wanted to bring
that process in the forefront in the foreground?
Just type fg, and you'll see it is running and
it is on the foreground and you will not get
your prompt back until it waits for 73 seconds.
Once it's finished in 73 seconds,
then we'll get a prompt back.
Anyway, let's Ctrl-c out of it, which
will completely stop, because if you type jobs
now, you won't see any jobs.
The next lesson, or sorry, the next topic
of this lesson is about process priority.
Which means run a process with a certain priority.
So you could just simply type nice, space,
-n, and space, what's the priority
you want to give it, -20 which is the
highest one, no I don't want it to mess
up my other processes in the system.
So I'll just say, simply say five
and then I'll do sleep -10.
I hit Enter.
Now what it's doing
is, it's telling CPU, hey, CPU I have
this process sleep ten and please give this
process priority five, which is not too high,
which is not too low,
it will tell that to the CPU, and CPU will process
that according to the command of nice, that's what nice is.
If you want to run your process to be the top
priority it is very important, then I would say run nice
-n -20, the name of the process, hit enter, and
CPU will say hey, that's it, I have nothing to do
but to take care of this process right away.
That's about the nice of the priority.
The other ones we already covered, which is the process
monitoring, and of course the list of process to see
what other processes are available in the system.