LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2004, 09:27 PM   #1
mikaelo
LQ Newbie
 
Registered: May 2004
Posts: 7

Rep: Reputation: 0
how do i kill a process from inside a bash script?


hi!

i'm a newbie at bash scripting but i think it's a powerful way of getting some things done.

anyway i'm writing a sort of `database' script and i'm using `find' to look for certain files. the problem is that find looks for all occurences of the file i'm looking for and so it takes too long to finish! is there some sort of way to kill `find' after it has located the first occurence of the file?

thanks!

-mike
 
Old 05-27-2004, 10:16 PM   #2
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
I don't use find, but you can try piping into head...

find file | head -n 1
 
Old 05-27-2004, 10:22 PM   #3
mikaelo
LQ Newbie
 
Registered: May 2004
Posts: 7

Original Poster
Rep: Reputation: 0
thanks for the reply. i can actually "work" on each line of `find's output using by pipint to `xargs' but then find would still be running in the background. what i'd need to do is actually stop find from executing after it has found the first item.

 
Old 05-28-2004, 04:39 AM   #4
Ma3oiS
LQ Newbie
 
Registered: May 2004
Posts: 12

Rep: Reputation: 0
Hi,

ranger_nemo advised you good. Try that command - it will output just first occurence of what you are looking for and then exit. It's because head -n 1 exits after processing first line of its input and therefore bash stops also command find.

Ma3oiS
 
Old 05-28-2004, 08:51 AM   #5
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
You can check it with time...
Code:
$ time locate png
/usr/X11R6/LessTif/doc/images/vh32.png
/usr/X11R6/LessTif/doc/images/lesstif-small.png
/usr/X11R6/LessTif/doc/images/lesstif.png
/usr/X11R6/LessTif/doc/images/lesstif-realsmall.png
/usr/X11R6/LessTif/doc/images/vh40.png

<< snip output >>

real    0m13.012s
user    0m0.562s
sys     0m1.674s

$ time locate png | head -n 1
/usr/X11R6/LessTif/doc/images/vh32.png

real    0m0.253s
user    0m0.013s
sys     0m0.030s
It takes my computer just over 15 seconds to display 23363 PNG files. Then, just over a quarter-second to display the first.
 
  


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
why my script die itself when I try to kill a process?? jaimese Linux - Newbie 4 10-27-2005 05:50 AM
small bash script to kill a PID flosch Linux - General 3 05-06-2004 09:28 PM
Kill a process launched by a shell script gsbarry Programming 7 01-06-2004 02:09 PM
BASH script inform user and kill process mounters Programming 3 02-11-2002 04:57 PM
script at xwin start to kill old process(es) butthead Programming 3 02-01-2002 08:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:49 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