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

  • SEARCH
  • Home
  • 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 6852293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:21:12+00:00 2026-05-27T01:21:12+00:00

I have a Java Webapplication running on Tomcat, executing Shell scripts at runtime in

  • 0

I have a Java Webapplication running on Tomcat, executing Shell scripts at runtime in which many “echo” commands are executed.

My problem is that I’d like all my logs to appear in a log4j RollingFileAppender, ie:

  • Java Log4j logs (which is easy to do)
  • Shell echo commands logs as well (which is the tricky part to me)

The Shell scripts are run via java.lang.Process class.
So far, I’ve managed to output the inputStream and errorStream of the process to System.out thanks to the log4j-provided StreamUtils.copy() method.

But then this is fine to get some ConsoleAppender output, but not a RollingFileAppender output.

Is there any convenient way to redirect the Process streams to a RollingFileAppender? In Log4j configuration or from the Java code?

Here is my LOG4J appender conf:

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <param name="Threshold" value="DEBUG" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ABSOLUTE} [%-20.20t] %-5p [%-25.25c{1}] - %m%n" />
    </layout>
</appender>

<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
    <param name="Threshold" value="DEBUG" />
    <param name="File" value="&LOG_DIR;/&PROJECT_NAME;.log" />
    <param name="Append" value="&APPEND;" />
    <param name="MaxFileSize" value="&MAX_SIZE;" />
    <param name="MaxBackupIndex" value="&MAX_BACKUP;" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} | &#x25;-21.21X{applicationId}|  %-40.40t| %-5p |%-25.25c{1}| - %m%n" />
    </layout>
</appender>

Here is my launching script code:

    ProcessBuilder pb = new ProcessBuilder("sh", "script.sh");
    Process p = pb.start();
    StreamUtils.copy(p.getInputStream(), System.out);
    StreamUtils.copy(p.getErrorStream(), System.out);
    int result = p.waitFor();
    LOG.info("Script ended with result " + result);
    return (result == 0);
  • 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-27T01:21:13+00:00Added an answer on May 27, 2026 at 1:21 am

    Are you using a thread? if so, you need to see https://stackoverflow.com/q/8229913/458901 to make it work with rolling appenders.

    In conjunction with that, your thread can use this method that you can use to pass in a string array (your command with args if any) and will return you the output of that command. Of course, just use the string it returns to add to your log 🙂

    private String execute(String[] command){
        //System.out.println(command);
        try{
            process = Runtime.getRuntime().exec(command);
            InputStream istream = process.getInputStream();
            Writer writer = new StringWriter();
            char[] buffer = new char[1024];
            Reader reader = new BufferedReader(new InputStreamReader( istream ));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
            reader.close();
            istream.close();
            return writer.toString();
        }
        catch ( IOException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
    

    }

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

Sidebar

Related Questions

I have a java web application (running on JBoss), which need to authorizate users
I have a Java web application running on Tomcat. I want to load static
I have a web application running in tomcat where I'm using a ThreadPool (Java
we have an internal webapplication running on tomcat, build on Spring. The webapplication front-end
I have a web application running on Tomcat 6 and a Java-programmed client application.
I have a java web application running under tomcat in a Sun java 6
Here's the scenario: I have a multi threaded java web application which is running
We have an web application running under JBoss 5 which periodically launches a 'java'
I have a Java program which runs in Tomcat and which needs to execute
I have a java web application running on tomcat, and will use single sign

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.