LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how do i kill a process from inside a bash script? (https://www.linuxquestions.org/questions/programming-9/how-do-i-kill-a-process-from-inside-a-bash-script-186747/)

mikaelo 05-27-2004 09:27 PM

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

ranger_nemo 05-27-2004 10:16 PM

I don't use find, but you can try piping into head...

find file | head -n 1

mikaelo 05-27-2004 10:22 PM

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.

:D

Ma3oiS 05-28-2004 04:39 AM

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

ranger_nemo 05-28-2004 08:51 AM

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.


All times are GMT -5. The time now is 07:11 PM.