Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to make a crontab job for killing any firefox processes running, if there is more than a max number of processes running, starting with the oldest process.

So let's say 5 processes is running and I only wan't to allow a maximum of 2, then kill the 3 oldest automatically.

share|improve this question
up vote 1 down vote accepted

Here goes bash script:


let COUNT=$(pgrep firefox | wc -l)
for (( i=1; i<=$COUNT-2; i++ ))
  PID=$(pgrep -o firefox)
  pkill $PID
# sleep 3
share|improve this answer
You can uncomment sleep line to add a timeout if this script is too fast and not killing processes as intended. – whtyger Jan 18 '13 at 14:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.