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 7793437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:29:17+00:00 2026-06-01T22:29:17+00:00

I’m working on a Java program that incorporates Process and Runtime to run several

  • 0

I’m working on a Java program that incorporates Process and Runtime to run several shell scripts for automated testing. All but one of these scripts runs fine, which causes an issue with script calls following it. Ex:

process = runtime.exec("A.sh");
process = runtime.exec("B.sh");
process = runtime.exec("C.sh");

A.sh runs fine and only takes a few seconds to run. B.sh, however, takes a couple minutes to run, and I think this is causing a problem with running C.sh, since they both interact with the same MySQL table and the overlap causes a Communications Link Failure.

Without overloading you with unnecessary information, my question is, how can I wait to ensure a run shell script has been run to completion/termination before moving on to the next exec() call?

What I’ve tried:

process.waitFor()

This doesn’t work, I don’t think it waits until the script is completely done

process.wait(long time_period)

This doesn’t work since it causes the current thread to wait which results in the remaining shell script calls to get skipped and the next test case to begin prematurely.

The shell script I call that causes the problem is not a simple script, but I didn’t write it myself and have little understanding of what it does behind the scenes. The only relevant information I have about it is that it directly connects to the MySQL database in question whereas my program uses java.sql.* to (I believe) remotely connect (although it is a local database on a remote machine).

Edit:

After following a suggestion, I’ve looked into the Apache Commons Exec and tried a new strategy, unsuccessfully.

ExecuteWatchdog watchdog = new ExecuteWatchdog(300000); //For five minutes
CommandLine cmdline = CommandLine.parse("./directory/shell.sh");
DefaultExecutor executor = setExitValue(0);
executor.setWatchdog(watchdog);
int exitVal = executor.execute(cmdLine);
//A line to log the exit val in another file

My log gives no implication that the shell script was actually run, as the time between a logged statement saying “shell.sh begins” and “test 2 starts” are essentially the same instant, which means the ~2 minute process that shell.sh runs never happens. Where did I go wrong?

  • 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-06-01T22:29:18+00:00Added an answer on June 1, 2026 at 10:29 pm

    I use Apache Commons Exec. Have synchronous and asynchronous execution support. Execution timeout can be set.

    First paragraph from their tutorial page:

    At this point we can safely assume that you would like to start some
    subprocesses from within your Java application and you spent some time
    here to do it properly. You look at Commons Exec and think “Wow –
    calling Runtime.exec() is easy and the Apache folks are wasting their
    and my time with tons of code”. Well, we learned it the hard way (in
    my case more than once) that using plain Runtime.exec() can be a
    painful experience. Therefore you are invited to delve into
    commons-exec and have a look at the hard lessons the easy way …

    Advanced usage example (some code is missing like BusinessException and “StreamUtil.closeQuietly”, but it could be easily replaced):

        ExecuteWatchdog watchdog = new ExecuteWatchdog(EXECUTION_TIMEOUT_IN_MS);
        DefaultExecutor executor = new DefaultExecutor();
    
        executor.setWatchdog(watchdog);
        executor.setExitValue(0);
    
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    
        executor.setStreamHandler(new PumpStreamHandler(outputStream, errorStream));
    
        try {
            log.info(commandLine.toString());
    
            int exitCode = executor.execute(commandLine, (Map<?, ?>) null);
    
            if (exitCode != 0)
                throw new BusinessException("Process exited with non-zero exit code.");
    
            return outputStream.toString();
        } catch (ExecuteException e) {
            String errorStreamStr = null;
    
            if (errorStream.size() != 0)
                errorStreamStr = errorStream.toString();
    
            StringBuilder errorMessageBuilder = new StringBuilder();
    
            errorMessageBuilder.append("main.error").append(":\n").append(
                    e.getMessage()).append("\n\n");
    
            if (errorStreamStr != null) {
                errorMessageBuilder.append("additional.error").append(":\n").append(errorStreamStr).append("\n\n");
            }
    
            errorMessageBuilder.append("command.line").append(":\n").append(commandLine.toString());
    
            if (log.isDebugEnabled())
                log.debug(errorMessageBuilder.toString());
    
            throw new BusinessException(errorMessageBuilder.toString());
        } catch (IOException e) {
            throw new IllegalStateException(e);
        } finally {
            StreamUtil.closeQuietly(outputStream, errorStream);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words

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.