[SOLVED] Executing and killing a process from sh file
Linux - NewbieThis 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!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
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:
top > ~/top.log&
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:
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:
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.
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.
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 12:20 PM.
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?