Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:21:18+00:00 2026-05-29T15:21:18+00:00

I have a jar that when run, goes through the files in a directory

  • 0

I have a jar that when run, goes through the files in a directory and processes 10 of them before exiting.

I have a shell script that looks something like this:

while true;
do java -jar myjar.jar
sleep2;
done

I have another shell script that runs the previous one on startup like so:

nohup loopscript.sh > /var/log/error.log

The problem is that sometimes the jar crashes when it needs more memory than the system has, and the entire loop seems to stop running. My log file ends with a stack trace when the memory cap is hit.

How can I reliably restart the loop after a crash? I read elsewhere on SO to do something like

until myserver; do
    echo "Server 'myserver' crashed with exit code $?.  Respawning.." >&2
    sleep 1
done

But this only works if myserver is itself in a loop, and I’m intentionally halting the jar after 10 runs to force garbage collection and reduce the chance of a crash midway. Is my logic flawed? Should I just put the jar into a loop and use the above method of restarting it when it crashes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:21:19+00:00Added an answer on May 29, 2026 at 3:21 pm

    As a quick and dirty solution, you can kill the process after some timeout. Here are two scripts in a parent-child relationship:

    • b.sh – parent

      echo Parent running
      while true; do
        ./a.sh &
        pid=$!
        echo Child running as $pid
        sleep 2
        if [ "`ps -p $pid`" != "" ]; then
          sh -c "/bin/kill $pid" >/dev/null 2>&1 
          echo Killed $pid
        fi
      done
      
    • a.sh – child

      echo Child running
      seconds=$RANDOM
      let "seconds %= 4"
      sleep $seconds
      echo Child finished
      

    However, as @Jim Garrison notes, it’s probably much better to design your app to run correctly, whatever that means in your case. This way, you can actually improve your app and see why you need that much memory. You’ll probably solve some cases which will pop up in the future, but are not visible because you are just “solving” the problem by restarting.

    It’s like playing Russian roulette – yes, you may get lucky 20 times in a row, but it’s going to happen…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a shell script file (run.sh) that contains the following: #!/bin/bash %JAVA_HOME%/bin/java -jar
If I have a jar that I need to run using java -jar FOO.jar
I have a .jar file that i've placed in my D:\Coldfusion8\wwwroot\web-inf\lib\ directory. The file
I have a codebase that I want to produce several deliverable jar files, each
In computer science I have learned that .jar files are basically a compressed set
I have a directory structure of package/bin and package/lib, where package/lib contains jar files
I have a jar file MapReduce that I'd like to run on s3. It
I have a jar file that I would like to execute using PHP, but
I have an executable jar Client.jar that requires jndi.properties file. Since the jndi properties
I have a 3rd party JAR that I have converted to an OSGI bundle

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.