newbie
newbie

Reputation: 321

How to run multiple commands repeatedly on centos?

I have many tasks, which are something like java -jar task1.jar and java -jar task2.jar and so on. All the tasks take different time to complete. I want to run them simultaneously and run again when completed. Is there a good way to solve the problem?

Upvotes: 0

Views: 101

Answers (2)

mauro
mauro

Reputation: 5940

You want to run both commands in background and restart them as soon as they will finish with a 10 minutes pause (your comments here below).

Something like this will restart each command independently:

$ while true ; do java -jar task1.jar ; sleep 600 ; done &
$ while true ; do java -jar task2.jar ; sleep 600 ; done &

If you want to wait both commands to finish before restarting them:

$ while true ; do
    java -jar task1.jar &
    java -jar task2.jar &
    wait
    sleep 600
  done

As per you comment I did add a 10 minutes pause before re-running the commands...

Upvotes: 1

Raju
Raju

Reputation: 2962

yes, you can run all those instructions in background mode

syntax

long_command with arguments > redirection &

Here in your case, it would be

java -jar task1.jar &
java -jar task2.jar &

Both will run in background mode and they will run in parallel.

To run the program 'n' number of times

#!/bin/sh

a=0
n=10

while [ $a -lt $n ]
do
    java -jar task1.jar &
    java -jar task2.jar &
    a=`expr $a + 1`
done

Infinite run with conditional break

#!/bin/sh

a=0

while true
do
   echo "Running "$a "th time"
   if [ $a -eq 5 ]
   then
      break
   else
      java -jar task1.jar &
      java -jar task2.jar &
   fi
   a=`expr $a + 1`
done

You can put your desired condition in if block.

Upvotes: 0

Related Questions