Kill a process after a certain amount of time

I would like to kill a process after a certain amount of time. Can I please get some ideas on how to do this?

Do you mean after a certain amount of time? You could record the pid of the process and start another background job that monitors the process with that pid and that will kill it when it runs too long..

Yes. Sorry for not making that clear. I edited my question. Can you fix the topic title also? I'm pretty sure the red means your a mod :).

Can you please give me a few ideas of how to do that?

shell:

#!/bin/bash
# this creates a child process that commits patricide after n seconds
n=30
sleep $n && kill  $PPID  &
# go merrily on your way
#   and  do stuff until you get killed by your kid.  Sounds like a soap opera.

You also should investigate the TMOUT environment variable

So if wanted to start firefox and then kill it shortly would do something like this?

#!/bin/bash
# this creates a child process that commits patricide after n seconds
n=30
firefox
sleep $n && kill  $PPID  &
# go merrily on your way
#   and  do stuff until you get killed by your kid.  Sounds like a soap opera.

Or would I name the script something like kill and do this?

firefox kill
kill firefox

Ok. You would have done better to have asked a complete question at the start. The example I gave you assumed that you started something like a shell script and wanted to time out the parent shell script. firefox is not a shell script, it is a standalone executable that runs in a child process so this method won't fly.

First off, kill [pid] is the way to stop a process. There may be variants of this like pkill for your OS - so what OS and shell are we talking about here?

Fedora and Bash.

try this:

#!/bin/bash
# alarm.sh
sleep $1
pkill firefox
exit 0

usage:
create an alias in your profile

alias firefox='/path/to/alarm.sh 30 &; /path/to/firefox'

At the command line type firefox. It will start the background process then run firefox.
If you do not want the alarm thing to happen type /path/to/firefox to bypass the alias.

Maybe you already have timeout . This program is more robust. Try typing timeout --help in your terminal.