How to run a program for a fixed period of time?

I am looking for a method built-in to ubuntu that will allow me to run a script or program or whatever for a fixed period of time.

I found a program that does this in a way I like, but the package is unavailable for Ubuntu. In any case, I was hoping for something built-in.

The only thing i can think of is to get the current time and set a cron job 30 minutes from 'now' that will kill the program. I was hoping there was a way to do this without setting up a script, but if I need to - it wont be the end of the world. After the 30 minute interval I would like to put my laptop in a sleep mode, but this can be separate from the timer thing.

Thanks in advance.


Why not use /usr/bin/timeout?

$ timeout --help
Usage: timeout [OPTION] DURATION COMMAND [ARG]...
  or:  timeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.

I've just wrote the following and it seems to work:

ping google.com& PID=$!; sleep 3; kill $PID

Of course you should substitute ping with the command you want to run and 3 with a timeout in seconds. Let me know if you need a more detailed explanation on how it works.