{ 9 comments… read them below or add one }

1 Professor Fapsanders 10.03.08 at 5:37 pm

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

2 Jadu Saikia 01.15.09 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

3 Riccardo Murri 02.25.09 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.

4 Muhammad 07.07.09 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

5 Peter 07.13.09 at 10:59 am

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

6 Muhammad 07.13.09 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

7 Peter 07.14.09 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

8 Cybergavin 11.17.09 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.

9 Chris 01.22.10 at 9:37 pm

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All