tags:

views:

57

answers:

1

Dear friends

I use the following command syntax to search params in my script

grep -qsRw -m1 "any_param" /dir/..../

Some times the search take avery long time The question is how to add time out to grep command

For example after 20 seconds grep will break out

If it not illegal to add time out to grep , how it will possible on other way?

THX Yael

+1  A: 

There is a Linux command timeout that can do this for you. Just run

timeout 20s grep -qsRw -m1 "any param" /dir/.../

EDIT: If you don't have access to timeout, you could try this:

grep -qsRw -m1 "any param" /dir/.../ & sleep 20; kill %1

Afterwards, the process ID of the grep command will be available as the Bash variable $!. (I assume you're running Bash... if not, it'd help to specify your shell in the question)

David Zaslavsky
[root@ ~]# man timeoutNo manual entry for timeoutNo my linux not have this command
yael
any way this command not good because I need the $? of the grep -:)yael
yael
I allany other creativ solutions?Yael
yael
@yael: running `man` does not tell you whether you have the command or not. Use `which timeout` instead.
David Zaslavsky
[root@ ~]# which timeout/usr/bin/which: no timeout in
yael
timeout not defined in our linuxyael
yael
If you are on a debian, `sudo apt-get install timeout`
fmark
I cant to installmaybe other solutionfor example to see which proccess is grep and then to kill it?
yael
How is it that you have root access but aren't able to install a program?Also my edit basically does what you're pushing for, i.e. finding the grep process and killing it. If that doesn't work for you, what's wrong with it?
David Zaslavsky