{ 14 comments… read them below or add one }

1 Professor Fapsanders October 3, 2008

You can also do it with bash using sleep and job control:
$ foo & sleep 10 && kill %1 && fg

Reply

2 Jadu Saikia January 15, 2009

Good one, I have to download and use it. Thanks.

The tip from Professor Fapsanders is also useful, we can make it more significant to kill the last background process by this:

$ sh foo & sleep 5 && kill $! && fg

// Jadu, unstableme.blogspot.com

Reply

3 Fred August 25, 2010

Great, thanks!

Reply

4 Riccardo Murri February 25, 2009

Warning: the tip from Professor Fapsanders only works if “foo” does not require any input (background processes cannot do that).

For the general case, you need to background the alarm process instead, as the sample “timeout” script does.

Reply

5 Linus September 30, 2010

$ sh foo & sleep 5 && kill $! & fg ### (note the last “&” is not “&&”, so you foreground the job immediately)

Reply

6 Muhammad July 7, 2009

How can i suppress the stupid ‘alarm clock’ output when the timeout expires?
I’m running “perl -e ‘alarm shift @ARGV; exec @ARGV’ 5 cat” it terminates the cat command after 4 seconds but then i get stupid ‘Alarm clock’ output on the screen. I have to use this command in a shell script and i don’t want this output.

I have tried redirecting standard error and std out but if i specify >/dev/null at the end of cat command, it redirects ‘cat”s output not from perl -e….. Any ideas? Thanks a lot

Reply

7 Peter July 13, 2009

Yup,
you can redirect the error output to stdout:
append ” >/dev/null 2>&1″ to the command

Reply

8 Muhammad July 13, 2009

Thanks Peter.

But the problem is appending >/dev/null 2>&1 to the command redirects stdout and stderr for the “command” not for the perl. So even by appending this to the command i still get “Alarm Clock” after command terminates.

For example perl -e ‘alarm shift @ARGV; exec @ARGV’ 5 foo arg1 arg2 > /dev/null 2>&1 ‘ will redirect output and stderr from ‘foo’ command not from the alarm command.
Thanks

Reply

9 Peter July 14, 2009

hey, you’re right… that’s funny.
I tried to dig some deeper using strace and funnily it does not show up then:

strace perl -e “alarm shift @ARGV; exec @ARGV” 5 cat >/dev/null 2>&1

interesting issue, I do lack enough time to investigate
regards

Reply

10 Cybergavin November 17, 2009

Thanks. Using the perl snippet is neat. Pretty much all UNIX systems come with bash and perl. So, this is cool.

Reply

11 Chris January 22, 2010

Thanks, the doalarm program worked very nicely for my purposes.

Reply

12 Pedro January 24, 2011

God bless you!

Reply

13 me February 12, 2011

There is a command “timeout” in the latest Ubuntu Linux (10.10).

Example:
date; timeout 10m foo; date

Reply

14 Bill July 12, 2011

Thanks, that works great!

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 6 + 2 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: