[Moved] script to kill a process in linux

Status
Not open for further replies.

gobiraj

Member level 2
Joined
Jul 23, 2013
Messages
46
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Visit site
Activity points
374
Hi All,

I am trying to create the script to kill a process for a periodic time. So I did write the following

step 1: pidof {process name}
step 2: I need to assign the process id to variable ( $ID ='pidof {process name}'}
step 3: kill the process using ID parameter which was assigned on step 2 ( kill $ID)

but it doesn't work .But I can't use process Id number with kill since id varies every time

So can some one please give me some suggestion
 

Re: script to kill a process in linux

You can use ps -e | grep <executable_name> to get the pid of the executable you need. You will need to extract the pid from the output of the grep command.
 

Re: script to kill a process in linux

ps -e | grep <executable_name> > list ( saved the output of 2nd command)

but I don't know how to extract the process id from the file which is on the 1st column

also once I extract it , how to use that value for kill command
 

Re: script to kill a process in linux

Use the "cut" command for getting the process ID.
Get this process ID into a variable.
Then use the value in the variable with the "kill" command to terminate the required process.

If any problem, get back.
 
Last edited:

Re: script to kill a process in linux

I did use following cmd and output

ps -e| grep {process name}

out put= 2070 ? timestamp processname

but number of digits of process id varies between 3-5 , so when can you suggest me how to use cut cmd in this situation

also once I extract the pid
then it needs to be assigned to variable then call that variable to kill cmd
so can you please let me know the way to call the variable with the kill cmd
 
You can use the '-d' and '-f' option in the cut command.

For assigning the output of a command to a variable use the command in back quotes(`)
eg : PID=`ps -e | grep a.out` (Still you need to use cut command to get your PID)

To use any variable in a command use symbol $ before the variable name.
eg: kill $PID

This will do.
I know the final code, but I want you to figure it out.
 

I did write the following

step 1- write the output of 'grep gedit ' on list file
step 2-extract the process id from the 1st column of the out put file and assign to PID variable
step 3- calling 'kill $ PID ' to kill the process

but I have some error showing up on line 3 ( - d not found)


#! /bin/sh

ps -e|grep gedit > list ( output = 2194 ? 00:00:05 gedit)


PID=cut -d " " -f 1 list

kill $PID
 

you could have directly pipe lined the output of the grep command to the cut command.

PID=`ps -e|grep a.out|cut -d " " -f 2`
kill $PID
 

Thanks brother for your great help...one more question when the number of digits of PID varies between 1-5 , I also need to change cmd `-f` according to that. So I am thinking of reslove this issue now. Let me try myself if not then I will approach you tomorrow. Mean time if you have any clue just reply to me.

Thanks again... sorry to keep you bothering.
 

You don't need to change the value of -f option accordingly. That option will only set which field to take i.e. After the 1st space take whatever is present till the next space. This delimiter is specified by the -d option.
 

Hi

Thanks fro your help and I figure it out.

Now I need to run the script in Linux ,which I created ,from windows PC. Also both windows PC and Linux PC are connected via intranet.

So I am going to place the script that I created on one of the directory in Linux PC.
Then that needs to executed from windows PC after the certain period of times ( which means some sort of automation calls from windows PC).

So would you please give me some ideas that I need to concern to finish this task. Then I can start working around one by one.

Thanks
Gobi
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…