Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

4 Answers 4

up vote 4 down vote accepted

Use something like

#!/bin/bash
java -jar <name-of-the-jar-file> &
javapid=$!
sleep <some-time-in-seconds>
kill $javapid

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

./<filename>

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 ( http://help.ubuntu.com/community/CronHowto ) 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:

#!/bin/bash 
COUNTER=0
while [  $COUNTER -lt 100 ]; do
     <same as above without the first line>
     let COUNTER=COUNTER+1 
done

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

share|improve this answer
1  
javapid = $! must be javapid=$! –  Michał Šrajer Sep 8 '11 at 13:40
1  
And you need an & to run java in the background –  psusi Sep 8 '11 at 13:44
1  
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

 
discard

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.