[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!
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In the above code, wen the command is evaluated, it doesnt go any further coz the top is running. Is there any way i can kill the process so dat the o/p is printed in the required file?
Top needs to be told that it is in batch mode and also how many times it must refresh. The following will do just that (run in batch mode and refresh once):
top -b -n 1
You also need to put the top command between $( and ), this way it is executed: text1=$(top -b -n1 > tmp.txt)
if you run a program in the background, e.g. with a & at the end then the $! variable will contain the pid of that forked process whcih you can then kill. Alternatively you can use a tool like pkill to kill it by name.
considering the above code as the example, instead of top consider sm other process is invoked which does not stop unless its done manually. if u try executing the code i wrote, although a temp.txt is created wid the expected o/p, the control does not proceed to cat statement coz top is still running. is there a way to kill this 'top' process from the code itself?
instead of creating a new process, can it b killed from the same code
smthin like:
#!/bin/bash
txt1="top > temp.txt"
eval $txt1
kill (wateva pid it is)
echo "\n\nContents of temp.txt....\n"
cat temp.txt
(this dsnt work coz the control never arrives at kill statement)
druuna...this code does not complete the execution becoz top is still running.
plz dont consider only the 'top' process. take any process 'pro1' which has to be started n terminated manually
Last edited by silverghost; 12-21-2010 at 06:47 AM.
the execution wont b compeleted. hw can this code be modified so that the code is fully executed? after the eval statement is called i want to kill the top process which has been invoked. can this be done?
Your code doesn't make any sense, which has been explained to you in the previous answers.
This: txt1="top > temp.txt" will store top > temp.txt into txt1. It will _not_ run top!!!
The code given by me in post #9 works. It will run top (once) in batch mode and stores the output in temp.txt. No evaluation is needed, no top process needs to be killed.
dude...what u said in post #9 works...i tried it. if i replace the top command with a process that doesnt stop by itself, can it be killed in mid execution?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.