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 have some runnable-jar files. I want to run them at regular intervals and also kill those processes after some time. I am a newbie when it comes to Ubuntu or linux in general. Can anybody suggest me the best practice to achieve it?

share|improve this question
up vote 4 down vote accepted

Use something like

java -jar <name-of-the-jar-file> &
sleep <some-time-in-seconds>
kill $javapid

in a file, make it executable (chmod +x ) and run it using


This will start the jar-file and kill it after some time. There are several ways to excecute the command at regular intervals. You could use cron ( ) to always excecut it as long the computer is runnig. Or use a simple shell script loop to start the execution from the command line:

while [  $COUNTER -lt 100 ]; do
     <same as above without the first line>

This will run the command 100 times, you could stop the execution simply by pressing ctrl-c.

share|improve this answer
javapid = $! must be javapid=$! – Michał Šrajer Sep 8 '11 at 13:40
And you need an & to run java in the background – psusi Sep 8 '11 at 13:44
I should stop scripting between other tasks :) - both correct of course – Thomas Sep 8 '11 at 13:48

The best way would be to create a shell script that runs the the executable, retrieves the PID of the process then kill it after a certain amount of time.

You can then add this as a job in your crontab to launch it at regular intervals.

share|improve this answer

This sounds like a job for cron.

share|improve this answer

For killing a process using kill command, we have to keep track of PID of the process to be killed. So, we have used pkill -f to kill the process. Now, I only have to remember the name of process and do not want to track the PID. The code for starting process remains the same. Hope it helps for somebody who comes here searching for the answer.

share|improve this answer

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.