I have to run a jar file with different parameters like this:

cd /Users/hawkarhama/Desktop/RUN 
echo $(java -jar IPM.jar 0 20 3 sign.txt)
echo $(java -jar IPM.jar 1 20 3 sign.txt)
echo $(java -jar IPM.jar 2 20 3 sign.txt)

If, say, the second one took more than five minutes, I want to stop it and continue to the next one. So the maximum time for each running jar is 5 minutes. If it takes less than that, that's fine; but if one runs longer than that, the script must continue to the next one.

  • (1) If you are asking specifically about a Java program that you can modify, one approach might be to build the time limit into the Java code. (2) Why do you say echo $(…) instead of just running the program with java -jar IPM.jar … sign.txt? – G-Man Says 'Reinstate Monica' Sep 3 '14 at 17:45
  • For the echo$you are right and I did that before that was just a copy of the code I putted here. As for the java code I don't know how to do that programmatically. I tried System.CurrentTimeMilles but it's not working. – Hk148 Sep 3 '14 at 17:49

This should work in bash 4 or later:

cd /Users/hawkarhama/Desktop/RUN 
( cmdpid=$BASHPID; (sleep 300; kill $cmdpid ) & exec java -jar IPM.jar 0 20 3 sign.txt )
( cmdpid=$BASHPID; (sleep 300; kill $cmdpid ) & exec java -jar IPM.jar 1 20 3 sign.txt )
( cmdpid=$BASHPID; (sleep 300; kill $cmdpid ) & exec java -jar IPM.jar 2 20 3 sign.txt )

For previous versions:

cd /Users/hawkarhama/Desktop/RUN 
bash -c '(sleep 300; kill $$) & exec java -jar IPM.jar 0 20 3 sign.txt'
bash -c '(sleep 300; kill $$) & exec java -jar IPM.jar 1 20 3 sign.txt'
bash -c '(sleep 300; kill $$) & exec java -jar IPM.jar 2 20 3 sign.txt'

After searching a bit for a pure java version, I found this:

ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); 
final Future handler = executor.submit(new Callable(){ 
executor.schedule(new Runnable(){
     public void run(){
}, 5*60*1000, TimeUnit.MILLISECONDS);
  • thanks for the answer.It still runs the process after the 5 minutes . kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]. It gives me this line after each process takes more than 5 minutes . and it still run it which is not what I want. – Hk148 Sep 3 '14 at 17:35
  • @Hk148: I got this to work, in a somewhat artificial test. (I got a kill: (1234) - No such process message for each invocation that finished before the time ran out.) Are you sure you're typing it correctly? (Try copying and pasting.) What operating system are you using? – G-Man Says 'Reinstate Monica' Sep 3 '14 at 18:07
  • This method has the dissadvantage of having the "kill process" and "no such process" output. I tried running it with a 2>&1 >/dev/null after the kill command, but it did not omit the output, so for clarity, I omitted that on the solution. I tested it on cygwin, not on a truly unix shell, but arguments for commands like kill should be the same as most linux boxes – NuTTyX Sep 3 '14 at 18:11
  • @G-Man I use Mac osx,but the problem is after the 5 minutes it won't start the next test, or the next line.after 5 minutes it just stops every thing. – Hk148 Sep 3 '14 at 18:53
  • @NuTTyX is there any other ways to do that in the java code? – Hk148 Sep 3 '14 at 18:58

If your system has the timeout command, you should be able to use it, as

timeout 300s java -jar IPM.jar 0 20 3 sign.txt
  • man timeout or info coreutils 'timeout invocation' to have a full help. It is a part of GNU coreutils. – Hastur Sep 3 '14 at 21:08
  • @G-Man When I try this it says, timeout: command not found. I think this version can not find the timeout or I don't have it on my mac – Hk148 Sep 4 '14 at 9:57
  • @Hk148: Yeah, after NuTTyX figured out that your version of bash was too old to support his first answer, I was afraid that OS X would be missing this, too. – G-Man Says 'Reinstate Monica' Sep 4 '14 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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