Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to limit the time a grep process command is allowed to run or be alive.

For example. I want to perform the following:

grep -qsRw -m1 "parameter" /var

But before running the grep command I want to limit how long the grep process is to live, say no longer than 30 seconds.

How do I do this?

And if it can, how do I return or reset to have no time limit afterwards.

share|improve this question
3  
didn't you ask this already? stackoverflow.com/questions/3061377/… –  unbeli Jun 17 '10 at 15:25
    
No this other quastion because I not get answer on the orig quastion Yael –  yael Jun 17 '10 at 15:25
4  
@yael: you got a perfectly good solution previously, using timeout, and you even got instructions on how to install it. It's rude to ignore this answer and then duplicate your question. –  Paul R Jun 17 '10 at 15:27
    
sorry but NO I do not want to install anything on my system THX yael –  yael Jun 17 '10 at 15:31
5  
@yael: then you need to update your original question to include this constraint, NOT just duplicate the question and start all over again –  Paul R Jun 17 '10 at 15:33

3 Answers 3

You can use timelimit, it is available in the default ubuntu or you could compile it yourself.

timelimit executes a command and terminates the spawned process after a given time with a given signal. A “warning” signal is sent first, then, after a timeout, a “kill” signal, similar to the way init(8) operates on shutdown.

share|improve this answer
2  
It's not in my Ubuntus (10.10 desktop, 11.04 server) so probably isn't a default. But easily grabbed with a quick sudo aptitude install timelimit. Works great once installed. –  Day May 6 '11 at 20:10

Here's some slightly hackish bash for that:

grep -qsRw -m1 "parameter" /var &

GREPPID=$!

( sleep 30; kill $GREPPID ) &

fg $GREPPID

Basically, it starts the search in the background, then starts a subshell that waits 30 seconds and then kills the search process. If the search takes <30s, the process will already be dead. It's not a great idea to kill a process that's already dead since there's a very small chance something else will reuse the pid, but for something like this it's generally okay. Lastly, the search is put back in the foreground and will run until it finishes or is killed.

share|improve this answer
    
how to get the $? from the grep in this case because in my script I take the $? from grep and verify if grep match the string? yael –  yael Jun 17 '10 at 15:29
    
I'm not sure there's a straightforward way to do that. Furthermore, this approach doesn't work in a script where bash doesn't have job control active. You may have to resort to a bit of Perl hackery to get the job done in a script. Hmm... –  Walter Mundt Jun 17 '10 at 15:31
    
it is possible to arrived this question to perl ? Yael –  yael Jun 17 '10 at 15:33

Create a new file called "tgrep" and put in it the following (or call it whatever you want):

#!/bin/bash
grep $@ &
PID=$!
sleep 30 && kill $PID 2>/dev/null &
wait $PID
exit 0

Next, run chmod a+x tgrep. Now if you want to use it you can type

./tgrep -i "results" /home/dan/*txt

Or you can just use it as tgrep (without the ./ and be able to use it from any directory) if you place it in a directory contained in your $PATH variable (like /home/dan/bin/, if that is in your $PATH variable, otherwise you can add it).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.