Now we will talk about checking process's status and killing it meaning or stopping it or exiting the
process.
So there are many processes in a Linux system that when you run it it doesn't stop gracefully or it
doesn't shut down gracefully and those processes most times are called like zombie process or a process
that does not have a parent or a parent process was started and it initiated a child process but then
that the parent process will stop but the child process was still lingering lingering around.
So those are the type of process that you do experience in your in your career in your career.
So you would have to actually go into the system find those processes and kill those processes.
So this is this topic or this lesson is about that process that you have to find out how can we automate
that process.
How can we put a script around that process to make it run it at certain times.
Wake up and say hey I see this process I'm going to go ahead and kill it.
So what are we going to do.
And this lesson is that we will create a dummy process anything that is just taking up some of your
memory resources will just create that process.
Then what we'll do is we will run the command to find out process id commands or different commands
that you could check like.
You could do mine C-f.
There are so many commands that you can run to check which processes running and then you could pipe
it to up to Grupp for specific process that you're looking for.
So in this last and this is a lab environment we will have a process that will double in the initiate
and then we will run the Madacy of command and group for that Pacific process.
And it's ideal.
And then of course we'll create a script around it and then we will kill the process in that script.
OK.
So this is this exercise will do.
So for that let's go ahead and log in to our linux server.
I'm logged in as myself and I am in my home.
I have saw PSC directory which is a professional script directory that I have created earlier anyway.
So first before I create a script I wanted to test my commands.
So I will run us a process a dummy process which is to sleep.
Sleep is just a command that will actually start taking your terminal and it will not give you your
prompt back for.
Let's see if I have six hundred here it means 600 seconds which is like 10 minutes.
So this will take away my prom or create a process for that prompt and we'll wait for 10 minutes and
then it will return my prompt back if I enter.
You see I do not have my problem back.
Now how can I check if this process what process IDs is assigned to it.
Who is running that.
Well you cannot do it from the same terminal because you do not have the prompt back unless you can
see out of it.
That's one way.
Second way is if you run the sleep process in the background then you could get your prompt back.
And then you will see the process ID but anyway will start a duplicate terminal session.
And now I will do P.S. mine to see if command to check the process and grep for sleep 600 and as you'll
see the first process right here is initiated by me the process ID process my user ID the process name
time the term started from the second one is just the grip self-command when we run the group for it.
See this group since since this group is also a process.
It runs at groups two groups itself.
How come it grips itself so fast.
But that's that's how it does in milli seconds anyway.
So we will run another command to go up out of group.
So filter out gruffed now only see my sleep command.
OK so this is the entire line.
I do not want to see the entire line.
I just want to get the process IDs so I could kill that process Heidi.
So for that we have to keep filtering it and will do.
Awk print dollar too because it's the second column.
And now I have a process.
So the process ID is this I want to kill that process id to kill this process id I'm just going to make
this bigger.
So this way everything can show up in one line before I kill it.
It's always a good idea that you tested that.
It comes back with the right I.D. So I'm going to used Exarchs human command X A R G S minus.
I parentheses.
This is where it holds the result.
Of this command.
And now when it holds that result in this parentheses.
What I want to do right now is I want to echo that whatever inside that parents.
I'll hit enter and you'll see it brings back the same process.
It didn't kill it.
Now I run a command kill right here and I could simply kill that command that process.
The process has gone.
And if you go back to your terminal in the background you'll see I got my prompt back and it says right
here terminated.
So this is one of the ways that you could actually create a script like that which will terminate your
process that are no longer needed.
So let's go ahead and create a process VI kill
process.
Ben Bash who's the author of course we all know that by now.
OK.
Date
description this script will find
dead or is zombie process seize and kill it.
Modified you ate you ate one.
OK.
So I'm just going to go ahead and save this and I will copy my entire command from here.
Go back into my script and paste it by right clicking and the puttee terminal.
So for that what I could do is just either I could just leave it like that
and and do echo all steeping processes are killed.
So this is how you could run that simple command and once you run it before you run it you have to give
it executable permissions kill process has executable permission perfect and I'm going to run that sleep
command in terminal behind me.
And now I will run the script kill process and also leaving process are killed.
Let's see if we got it from Baccus terminal.
So that's one way to do it.
The second way I'm sure you know I want you to try that second.
I'm not going to tell you is to use for a loop.
So you could actually put this entire command until here into a variable Let's call that variable A
and then you will run the for loop for it for I in dollar a do kill.
Dollar a done.
So try that and I'm sure it will work.
And if you have any issue with this for loop command and you're trying this let me know but I'm sure
you will be able to write both scripts with arguments and hope.