-   Programming (
-   -   how do i kill a process from inside a bash script? (

mikaelo 05-27-2004 09:27 PM

how do i kill a process from inside a bash script?

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?



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.


Ma3oiS 05-28-2004 04:39 AM


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.


ranger_nemo 05-28-2004 08:51 AM

You can check it with time...

$ time locate png

<< snip output >>

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

$ time locate png | head -n 1

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 03:13 AM.