user1848647
user1848647

Reputation: 9

Running a jar file (within a loop) in a script with redirected output

I'm trying to run a jar file multiple times (in a loop) and redirect its output to a file (using the append >> operator). I'm running, and must keep using, Windows 7. I tried doing this in a windows batch file and ran into the problem below so I installed Cygwin to make use of the bash script. My script is below:

for i in {1..10..1}
  do
    echo "Run $i"
    java -jar myjar.jar -cl >> runresults.txt
    echo "Sleeping..."
    sleep 60
    echo "Awake!"
done

The problem: The script (Windows batch or Cygwin bash) runs only some iterations before hanging up (usually never more than 3). There's no error given. I added the sleep command to ensure that the prior iteration had time to release any locks prior to any attempt to make the next run. I've increased the sleep time to 200+ seconds and the behavior is still the same. Can anyone help me out on this?

Upvotes: 0

Views: 2611

Answers (1)

Ali Okan Yüksel
Ali Okan Yüksel

Reputation: 388

if [ ! -f runresults.txt ]; then
    touch runresults.txt
fi

for i in {1..10}
  do
    echo "Running... $i"
    java -jar myjar.jar >> runresults.txt &
    echo "Sleeping 2 seconds..."
    sleep 2
done

Upvotes: 1

Related Questions