简体   繁体   English

进程循环运行,而不是每次都记录日志

[英]Process run in loop not logging every time

I have a class in which I can run rsync commands from a shell script. 我有一类可以从shell脚本运行rsync命令的类。

It all works as it should, except when logging the process output (instream and error stream) to file. 除了将流程输出(流和错误流)记录到文件中之外,所有这些都应按预期进行。

I have a loop in my class which allows 5 runs if the process fails, and in each run every stream/writer/reader is initialised and closed completely. 我的类中有一个循环,如果进程失败,则允许5次运行,并且在每次运行中,每个流/写入器/读取器均被初始化并完全关闭。

The problem is that the file is only written to once or twice in the 5 runs, and it's never the same runs that are output. 问题在于,在5次运行中,该文件仅被写入一次或两次,并且永远不会输出相同的运行。 I set a 10 second sleep after the process to make the output more readable and I could see the only output being written to file was after 20 and 30 seconds, or after 10 and 40 seconds etc. 我在该过程之后设置了10秒钟的睡眠时间,以使输出更具可读性,并且可以看到唯一写入到文件的输出是在20秒和30秒之后,或者在10秒和40秒之后等等。

Every output from the process in written to console but not to file. 进程的每个输出都写入控制台,但不写入文件。

My code is as follows. 我的代码如下。 Any advice would be appreciated. 任何意见,将不胜感激。

private static void run() throws IOException {
    while (retry && NUMBER_OF_TRIES >= tries){
        InputStream in = null;
        PrintStream out = null;
        InputStream errIn = null;

        try {
            // get runtime object to run cmd
            Runtime RT = Runtime.getRuntime();

            Process PR = RT.exec( cmd );
            errIn = PR.getErrorStream();
            in = PR.getInputStream();
            out = new PrintStream(new FileOutputStream(new File(output), true));
            System.out.println("file output initialised");
            StreamGobbler inConsumer = new StreamGobbler( in, new Date() + " OUTPUT: ", out );
            StreamGobbler errConsumer = new StreamGobbler( errIn, new Date() + " ERROR: ", out );
            System.out.println("Starting consumer threads");
            inConsumer.start();
            errConsumer.start();

            Thread.sleep(10000);

            int exitVal = PR.waitFor();
            if (exitVal > 0){
                retry = true;
                tries++;
            } else {
                retry = false;
            }
            System.out.println("ExitValue: " + exitVal);

            // Wait for the consumer threads to complete
            inConsumer.join();
            errConsumer.join();         

        } catch ( Exception e ) {

            e.printStackTrace();
            System.out.println( "failed: " + e.getMessage() );
        } finally {
            // the StreamConsumer classes will close the other streams
            if ( out != null ){
                out.close();
            }
            if (in != null){
                in.close();
            }
            if (errIn != null){
                errIn.close();
            }               
        }
    }
}

i don't ever see your printstream object "out" ever being used besides it getting initialized. 除了初始化外,我再也没有看到过使用过的printstream对象。 If you want to write to the file you should call the printstream object. 如果要写入文件,则应调用printstream对象。

System.out.println("ExitValue: " + exitVal);
//now call the printstream object
out.print("ExitValue: " + exitVal);
//flushing will force the string to be written to the file
out.flush();

You can follow the above code everytime you write to console and you want to write to the file you created your printstream object with. 每当您写入控制台并想写入创建了printstream对象的文件时,都可以遵循上述代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM