How to kill top command using script?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to kill top command using script?
# 1  
Old 08-04-2008
How to kill top command using script?

Hi all,

I am using top command in my script to redirect output to temp file. I used kill -9 `ps -ef|grep top|grep -v grep|awk '{print $2}'` to kill top command in my script, but it is not working? Can you please tell how to kill top command in my script?
# 2  
Old 08-04-2008
Why do you have to kill the top command in such manner, how are you using that command, can we see the relevant snippet of this script ? Else, you can always use "pkill -9 top" but be careful, this will kill any process with " * top * " pattern in the name, it's pretty harsh, so watch out.
# 3  
Old 08-04-2008
If you are running it in the background, just store $! in a variable just after starting it, and kill $! when you need to.

top has options to tell how many iterations to run; perhaps you should be using those instead.

Don't use kill -9 unless you know what you are doing.
# 4  
Old 08-04-2008
Quote:
Originally Posted by johnl
Hi all,

I am using top command in my script to redirect output to temp file. I used kill -9 `ps -ef|grep top|grep -v grep|awk '{print $2}'` to kill top command in my script, but it is not working? Can you please tell how to kill top command in my script?
Use the -f option to top...it outputs one display to the named file and exits automatically.

Code:
top -f /path/to/outfile

# 5  
Old 08-04-2008
Quote:
Originally Posted by shamrock
Use the -f option to top...it outputs one display to the named file and exits automatically.

Code:
top -f /path/to/outfile

depends on what version of top
in my end, the option is
-b (for batch)
and -n 1 (for 1 iteration)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Advanced & Expert Users

Trapping a kill command sent to a script and using it to send an EOF to a subprocess before killing

I originally had a script written in pure shell that I used to parse logs in real time and create a pipe delimited file that only contained errors. It worked but it was using a lot of memory (still not clear on why). I originally got around this by writing a wrapper for the script that ran on cron... (1 Reply)
Discussion started by: DeCoTwc
1 Replies

3. Shell Programming and Scripting

Shell Script to grep output from top command.

Hello, I want a script which would grep details from top command for specific processes. I m not sure of the PID of these processes but i know the names. $ top -c top - 16:41:55 up 160 days, 5:53, 2 users, load average: 9.36, 9.18, 8.98 Tasks: 288 total, 9 running, 279 sleeping, 0... (8 Replies)
Discussion started by: Siddheshk
8 Replies

4. Shell Programming and Scripting

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: #!/bin/sh top -n 1 -u mysql | grep mysqld | awk '{print $1}' > pid proc='cat pid' kill -9 $proc or i try with kill -9 `top -n 1 -u mysql | grep mysqld... (8 Replies)
Discussion started by: chandro
8 Replies

5. Shell Programming and Scripting

Script which uses “kill -9” command

i have one script which uses “kill -9” command. That prevents from getting the process core dumps. Apparently once tomcat lands in a confused state, we seem to have no other option, other than Kill -9. is there any other way to get rid of this. Script: sleep 2 PID=`ps -ef | grep "^tomcat... (3 Replies)
Discussion started by: Amrutayan09
3 Replies

6. Shell Programming and Scripting

Need help with shell script - output of top command

I have written shell script to send file as an attachemt of email and output of "top -o res" command as email body. it works fine if i execute manually from prompt but it does not send "top -o res" command output in email body when it is executed via crontab. Any suggestions. My script is below:... (5 Replies)
Discussion started by: needyourhelp10
5 Replies

7. Shell Programming and Scripting

Help with script to stop a user giving kill command on a server!!

Hi, I am new to shell scripting and want to create a script with the follwoing description: I want to restrict the users from giving a kill command on a unix server. The server have a restricted logins with login id and passwords. I want a script that will find out if a user has given a... (9 Replies)
Discussion started by: shell_scripting
9 Replies

8. Shell Programming and Scripting

Perl script to kill the process ID of a command after a certain period

All, I am trying to build a script in perl that will alllow me to pass the IP address to a ping command and redirect the output to a file and then kill that process after a certain period of time. let's say, I call my script ping.pl, I would like to be able to run it like this for example :... (7 Replies)
Discussion started by: Pouchie1
7 Replies

9. AIX

Top command in AIX 4.2 (no topas, no nmon, no top)?

Is there a 'top' command equivalent in AIX 4.2 ? I already checked and I do not see the following ones anywhere: top nmon topas (1 Reply)
Discussion started by: Browser_ice
1 Replies

10. Shell Programming and Scripting

Using of Top command as script parameter

Hi All I am interested at using the output of Top command which shows some important system paramter related to CPU usage. I want to write a shell script in order to manipulate this output of command and to take some statistics for CPU. Basically, I tried to redirect the top output to a... (4 Replies)
Discussion started by: elthox
4 Replies
Login or Register to Ask a Question