kill process from a file or directly with top


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting kill process from a file or directly with top
# 1  
Old 02-25-2011
Question kill process from a file or directly with top

i have edited a script to kill an exact mysql process is causing the high load on the server, my problem is, kill dont kill it!

script:
Code:
#!/bin/sh
top -n 1 -u mysql | grep mysqld | awk '{print $1}' > pid
proc='cat pid'
kill -9 $proc

or i try with
Code:
kill -9 `top -n 1 -u mysql | grep mysqld | awk '{print $1}'`

and none works.

the first one tell me.
Code:
toot@server [~]# sh kill.sh
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

and the second one
Code:
toot@server [~]# kill -9 `top -n 1 -u mysql | grep mysqld | awk '{print $1}'`
-bash: kill: 17720: arguments must be process or job IDs

how to fix it, and what im doing wrong???

thank you.

Last edited by Scott; 02-25-2011 at 01:09 PM.. Reason: Please use code tags
# 2  
Old 02-25-2011
What Operating System and version are you running?
I assume from the error message that /bin/sh is a link to bash on your system.

I don't think that "top" is suitable for this exercise because of the amount of irrelevant screen control characters etc. you will get. The formatted output from "top" is unsuitable for processing in unix Shell.
Really needs to use the "ps" command (not "top") combined with some base logic to decide if the process is rogue.


In general, never issue "kill -9" unless you are having trouble shutting a system down. Certainly never issue "kill -9" to a database process while the database is up.

Btw. The first script contains an error (wrong sort of quotes) but this does not mean that the script will work:
Quote:
proc='cat pid'
# 3  
Old 02-25-2011
im using linux centos 5.5

for PS can use
Code:
ps -ef|grep -v grep |grep mysql| awk '{print $2}'

but give me 2 process.. the mysq1d and mysql

yes i know is bad to kill a process of a DB while database is running, but its the only way for now to make load get down.

i need to optimize database then.

Last edited by Scott; 02-25-2011 at 01:32 PM.. Reason: Code tags
# 4  
Old 02-25-2011
The output of top is formatted.
When trying to reproduce your problem your
Code:
top -n 1 -u mysql | grep mysqld | awk '{print $1}'

command returns an empty strings. So that's why your kill asks for a PID.

try this one:
Code:
kill -9 $(top -bn1 -u mysql | grep mysqld | cut -c1-5)

The trick is in the b argument of top that removes the formatting.

But top continue to show the pid using the 5 first characters, so you can't use a simple
Code:
awk '{print $1}'

to get the pid. I suggest using the
Code:
cut -c1-5

instead
This User Gave Thanks to Dahu For This Post:
# 5  
Old 02-25-2011
Quote:
for PS can use ps -ef|grep -v grep |grep mysql| awk '{print $2}' but give me 2 process.. the mysq1d and mysql
Any reason to not use "ps -fu ....":
Code:
ps -fumysql|grep -v "grep" |grep "mysqld"| awk '{print $2}'

# 6  
Old 02-25-2011
with my command i get this:
Code:
toot@server [~]# ps -ef|grep -v grep |grep mysql| awk '{print $2}'
16942
30862
toot@server [~]#

with your solution works smoothly...

i run that script on another i had, on crontab each 5 min checks if the load is bigguer than X then execute that script and email me server info, hostname and uptime,

thank you very much!!!

CTRL+X

---------- Post updated at 01:32 PM ---------- Previous update was at 01:30 PM ----------

Quote:
Originally Posted by methyl
Any reason to not use:
Code:
ps -ef|grep -v "grep" |grep " mysqld "| awk '{print $2}'

that line dont show process id:
Code:
toot@server [~]# ps -ef|grep -v "grep" |grep " mysqld "| awk '{print $2}'
toot@server [~]#

instead the first one i post
Code:
toot@server [~]# ps -ef|grep -v grep |grep mysql| awk '{print $2}'
18718  <- mysql database
30862  <-mysqld
toot@server [~]#

the code of Dahu solve my problem Smilie

Last edited by Scott; 02-25-2011 at 01:40 PM.. Reason: Use code tags, please...
# 7  
Old 02-25-2011
cut and awk have the exactly the same problem when handling the formatted output of top command.

As Dahu stated : the trick is to use the b option of the top command, then, either cut or awk behaves as expected.

Save some pipe :
Code:
ps -ef | awk '/"[m]ysqld"/{print$2}'

Code:
kill -9 $(top -bn1 | awk '/"[m]ysqld"/{print$1}')

!!

Last edited by ctsgnb; 02-25-2011 at 02:07 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies

2. Shell Programming and Scripting

How to kill the process when the file is locked?

I was trying to read the file to create a table in SAS and I got error as follows while I read. Resource is write-locked by another user. File =/usr/sas/sas_config/Lev1/SASApp/StoredProcessServ​er/Logs/SASApp_STPServer_2015-09-29_tmp_18208.log. System Error Code = 0. ERROR: File is in... (10 Replies)
Discussion started by: Ram Kumar_BE
10 Replies

3. Shell Programming and Scripting

Soft kill a process to redirect the last kbytes output to a file

Hey guys, I have a python script that I call with this line: python mypythonscript.py >> results.csv &The problem is that the redirection from the stdout to the file results.csv only writes 4096 kbyte blocks. So if i kill this process with kill the last kbytes that the script produce will... (6 Replies)
Discussion started by: Mastaer
6 Replies

4. Shell Programming and Scripting

Kill top 5 memory uses process

Hi All, how to kill 5 top memory used process in my hp-ux. Thanks, Kki (9 Replies)
Discussion started by: kki
9 Replies

5. Shell Programming and Scripting

Kill an specific process ID using the KILL and GREP commands

Good afternoon I need to KILL a process in a single command sentence, for example: kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'` That sentence Kills the process ID corresponding to the program CAL255.4ge. However it is possible that the same program... (6 Replies)
Discussion started by: enriquegm82
6 Replies

6. UNIX for Dummies Questions & Answers

top output for a specific process to a file

Hi, I have the following script, and it is driving me nuts. It just hangs, I've tried all kinds of tricks, but it won't work. I know it can, it is pretty straight forward. It hangs when it tries to output $X Any help appreciated! #!/bin/bash set -o xtrace command="top -b"... (10 Replies)
Discussion started by: Bloke
10 Replies

7. Linux

Kill a process without using kill command

I want to Kill a process without using kill command as i don't have privileges to kill the process. I know the pid and i am using Linux 2.6.9 OS. (6 Replies)
Discussion started by: sudhamacs
6 Replies

8. UNIX for Advanced & Expert Users

When kill doesnt work, how to kill a process ?

Hi All, I am unable to kill a process using kill command. I am using HP-UX system. I have tried with kill -9 and i have root privilages. How can i terminate this daemon ? ? ? Regards, Vijay Hegde (3 Replies)
Discussion started by: VijayHegde
3 Replies

9. Shell Programming and Scripting

how to kill process from file

i have a script that read a file which contains process_id and time that he's in and it lookes like this 0:30 54545 0:44 66788 0:90 23233 i need to read every line in the file and get the time and if the process is greater then 0:30 to kill the process id the script looks like... (17 Replies)
Discussion started by: naamas03
17 Replies

10. Post Here to Contact Site Administrators and Moderators

how to kill process from file

hello i want to write a script in unix which read file that contains : hour and process_id like this file bbb: 0:30 6678 1:40 8789 and for every line he check if the hour greater then 30 minutes he kill the process_id the script looks like this cat bbb | while read line do ... (1 Reply)
Discussion started by: naamas03
1 Replies
Login or Register to Ask a Question