LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 05:02 AM   #1
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Rep: Reputation: 0
Question Executing and killing a process from sh file


Hi,
i have recently started a bit of sh coding. i was trying out sm commands from an sh file.

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

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?

Thank You!
 
Old 12-21-2010, 05:19 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i think you just want to read the top manpage to do a single shot. "top -n 1" for example.
 
Old 12-21-2010, 05:20 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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)

Hope this helps.
 
Old 12-21-2010, 05:36 AM   #4
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
thx for replyin. what if the process, which will be invoked after the evaluation, has to be killed manually? Is there any way to do dat?
 
Old 12-21-2010, 06:00 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 12-21-2010, 06:01 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Have a look at the kill command.

kill 1234 will kill the process with PID 1234.

Hope this helps.
 
Old 12-21-2010, 06:22 AM   #7
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
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?
 
Old 12-21-2010, 06:25 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
wid? dat? cos? pfffft.
 
Old 12-21-2010, 06:29 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Your original code does not work, you need to implement the advise given by both acid_kewpie and me.

This works:
Code:
#!/bin/bash
top -b -n 1 > temp.txt
echo "Contents of temp.txt...."
cat temp.txt
And please spell out your words.
 
Old 12-21-2010, 06:34 AM   #10
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
m sry for not being able to say wat i wanna do

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.
 
Old 12-21-2010, 06:45 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

1) Which part of spell out your words don't you understand??

2) You are posting a (none working) solution and _not_ the problem you are having.

Tell is, in understandable English, what it is you are trying to accomplish.
 
Old 12-21-2010, 06:54 AM   #12
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
my bad...i will try to be specific

run this code...

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

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?
 
Old 12-21-2010, 07:01 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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.
 
Old 12-21-2010, 07:08 AM   #14
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
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?
 
Old 12-21-2010, 07:23 AM   #15
silverghost
LQ Newbie
 
Registered: Dec 2010
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
This: txt1="top > temp.txt" will store top > temp.txt into txt1. It will _not_ run top!!!
execute the code in the terminal. open up another terminal n run 'top' command. it will show u 2 'top' processes.
 
  


Reply



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 06:30 AM
how to close open file without killing process patcheezy Linux - Newbie 3 05-13-2009 03:00 PM
How to find the process ID of process executing in the background akeneche Linux - Newbie 1 01-03-2008 10:08 AM
Killing a process by its name rabeea Linux - Networking 3 03-17-2005 05:30 AM
Really Killing a Process! lazlow69 Linux - Newbie 14 05-10-2003 10:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:54 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
Open Source Consulting | Domain Registration