LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-21-2010, 08:57 AM   #16
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387

Hi,
Quote:
Originally Posted by silverghost View Post
execute the code in the terminal. open up another terminal n run 'top' command. it will show u 2 'top' processes.
I don't have to open a second terminal to run top. I immediately have my prompt back after executing txt1="top > temp.txt" and as a result txt1 is filled with top > temp.txt (As in: Not the output of the top command but the literal text between the double quotes).

This fills the txt1 variable with the text between double quotes (not the output of top): txt1="top > temp.txt"

This will run top (and you will not get your prompt back until pressing ctrl-c. txt1 will be empty, temp.txt will hold the output of top): txt1=$(top > temp.txt)

This will run top once, store the ouput in temp.txt (the variable txt1 stays empty and after a second or so you have the prompt back): txt1=$(top -b -n1 > temp.txt)

If you see 2 top processes when running top, there must be another top running, but it is not created by executing txt1="top > temp.txt" because that does not start top.
 
Old 12-21-2010, 10:08 AM   #17
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
You want to launch a process in the background, someone told you above that's done by appending '&' to it.

You want to know its pid so you can kill it later, someone told you above that's in $!.

You want an actual command to kill $pid, that's "kill -9".

Putting it all together is simple enough:

Code:
top > ~/top.log&
pid_whatever=$!
sleep 10s
kill -9 $pid_whatever
But, as others already told you above as well, top in interactive mode won't help you much. It's not designed to output to a file, but to a tty, and you'll get an empty file. You should really follow their advise and use -b and -nX if you want to dump the output to a file. Just make X as big as you want. What you really want is this:

Code:
top -b -n 100 > ~/top.log
If you really want to control the timing yourself you can use an high number, and kill it yourself:

Code:
top -b -n 1000000 > ~/top.log&
pid_whatever=$!
sleep 10s
kill -9 $pid_whatever
 
Old 12-21-2010, 11:33 AM   #18
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Watch out when using kill -9 pid. This could have some serious side effects you do not want!

When at all possible use kill PID and only if that does not kill the intended process use kill -9 PID

The -9 flag tells kill to kill the process no matter what and you could end up with a corrupted database when used on mysql for example.

A normal kill gives the process time to do the following (kill -9 does not always allow for this):
- delete any temporary files,
- shutdown sockets,
- remove shared memory segments,
- close open files or some other task.

In short: Use kill PID. Only use kill -9 PID if you really have to.

Hope this clears things up.
 
Old 12-21-2010, 11:42 AM   #19
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
killing -9 top wouldn't have any bad effect, though. Much less when it's being ran in batch mode. At most you could get a partial output in the last batch dump to the log file. But yes, generally, you should use kill, and -9 only if it fails and you are sure that there won't be problems.
 
Old 12-21-2010, 12:55 PM   #20
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
is there any way to kill the process other than using the options -b and -n?
i may sound like a paranoid ignoring the modes which are already given. but the thing is m not able to explain wat actually i want. so if u can tell me a way to kill the 'top' process after it is invoked using the eval statement

Last edited by silverghost; 12-21-2010 at 01:20 PM.
 
Old 12-21-2010, 01:20 PM   #21
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Did you have a look at i92guboj's examples in post #17?
 
Old 12-21-2010, 01:35 PM   #22
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
o yes...i did try to put it into sh. code is as follows:

#!/bin/bash
txt1="top > temp.txt&"
pid_pro=$!
eval $txt1
kill -9 $pid_pro
echo "\n\nContents of temp.txt....\n"
cat temp.txt

it sayz failed tty. to be precise this is the o/p i got:

chaitanya@ubuntu:~$ sh temp.sh
kill: 5: Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or
kill -l [exitstatus]


Contents of temp.txt....

chaitanya@ubuntu:~$ top: failed tty get

^C
chaitanya@ubuntu:~$

i had to stop it using ctrl+c.
 
Old 12-21-2010, 01:46 PM   #23
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
m asking for an alternate method because if top is replace by some other process which interactive just like 'top' then can it be killed while its being executed.
 
Old 12-21-2010, 01:52 PM   #24
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Code:
#!/bin/bash
top -b -n 1000 > temp.txt &
pid_pro=$!
sleep 1
kill $pid_pro
echo "Contents of temp.txt...."
cat temp.txt

Last edited by druuna; 12-21-2010 at 01:56 PM. Reason: Removed the -9 flag
 
Old 12-21-2010, 02:01 PM   #25
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
its workin jst the way i wanted.

thx a lot...for the patience, co-operation n for listening to me all the way even though i was a headache :P

Thx to every1 who helped me get thru wid it.
 
Old 12-21-2010, 02:29 PM   #26
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
i would like to ask 1 more thing. does the sleep statement specify the time after which the process should be killed?

Last edited by silverghost; 12-21-2010 at 02:33 PM.
 
Old 12-21-2010, 05:34 PM   #27
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by silverghost View Post
o yes...i did try to put it into sh. code is as follows:

#!/bin/bash
txt1="top > temp.txt&"
pid_pro=$!
eval $txt1
kill -9 $pid_pro
echo "\n\nContents of temp.txt....\n"
cat temp.txt

it sayz failed tty. to be precise this is the o/p i got:
No wonder. Please, read carefully: $! gives you the PID of the last command you ran. You are assigning a value to a variable and then capturing $!, you didn't run any command, so you get no PID, hence kill will fail because it needs a PID.

May I ask, why oh why are you using this scheme at all?

Code:
var="somecommand&"
eval $var
When you could just do

Code:
somecommand&
?!?
 
Old 12-21-2010, 05:37 PM   #28
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by silverghost View Post
i would like to ask 1 more thing. does the sleep statement specify the time after which the process should be killed?
All the info you needs is in the sleep man page

Code:
$ man sleep
In the examples above, sleep is used as a counter, after sleeps returns the next command will be ran (kill, in my example). Read the man page for more info on how sleep works.
 
Old 12-22-2010, 12:12 AM   #29
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
yea...i din kno dat...druuna helped me out over there.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Killing More than one process ruud Programming 11 04-27-2010 07:30 AM
how to close open file without killing process patcheezy Linux - Newbie 3 05-13-2009 04:00 PM
How to find the process ID of process executing in the background akeneche Linux - Newbie 1 01-03-2008 11:08 AM
Killing a process by its name rabeea Linux - Networking 3 03-17-2005 06:30 AM
Really Killing a Process! lazlow69 Linux - Newbie 14 05-10-2003 11:31 PM


All times are GMT -5. The time now is 06:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration