{ 16 comments… read them below or add one }

1 Professor Fapsanders October 3, 2008 at 5:37 pm

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 at 10:16 am

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 at 8:51 am

Great, thanks!

Reply

4 Riccardo Murri February 25, 2009 at 2:15 pm

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 at 4:23 pm

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

Reply

6 Muhammad July 7, 2009 at 8:05 pm

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 at 10:59 am

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

Reply

8 Vikrat January 21, 2012 at 3:04 am

even m facing the error, in the code below

#include
#include
#include
using namespace std;
void onAlarm(int a)
{
cout<<"Alarm Buzzz\n";
sleep(2);
exit(0);
}
int main()
{
signal(SIGALRM, onAlarm);
cout<<"enter\n";
alarm(3);
execl("./sant/a.out","./a.out",(char*)0);
}

if the file a.out has a sleep of say 10 seconds
then it wont execute the onAlarm() function, instead it would just show Alarm clock
can u help?

Reply

9 Muhammad July 13, 2009 at 2:01 pm

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

10 Peter July 14, 2009 at 6:22 am

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

11 Cybergavin November 17, 2009 at 6:27 pm

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

Reply

12 Chris January 22, 2010 at 9:37 pm

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

Reply

13 Pedro January 24, 2011 at 4:12 am

God bless you!

Reply

14 me February 12, 2011 at 11:13 pm

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

Example:
date; timeout 10m foo; date

Reply

15 Bill July 12, 2011 at 5:42 pm

Thanks, that works great!

Reply

16 Amar February 1, 2013 at 6:56 am

thanks a lot !

the do alarm program works fine for me.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , ,

Previous Faq:

Next Faq: