Not reading after initial file in tail -f <file> | head -n 5

I’m stuck on Stage #BR6

Here are my logs:

tester::#BR6] Running tests for Stage #BR6 (Pipelines - Dual-command pipeline)
[tester::#BR6] [setup] export PATH=/tmp/strawberry/mango/pineapple:$PATH
[tester::#BR6] Running ./your_program.sh
[tester::#BR6] [setup] echo "blueberry grape\npear orange\nbanana mango\npineapple strawberry\napple raspberry" > "/tmp/quz/file-41"
[your-program] $ cat /tmp/quz/file-41 | wc
[your-program]        5      10      78
[tester::#BR6] ✓ Received expected response
[tester::#BR6] [setup] echo "1. orange pear\n2. strawberry mango\n3. pineapple apple" > "/tmp/bar/file-99"
[your-program] $ tail -f /tmp/bar/file-99 | head -n 5
[your-program] 1. orange pear
[your-program] 2. strawberry mango
[your-program] 3. pineapple apple
[tester::#BR6] ✓ Received redirected file content
[tester::#BR6] Didn't find expected line.
[tester::#BR6] Expected: "4. raspberry banana"
[tester::#BR6] Received: "" (no line received)
[tester::#BR6] Assertion failed.
[tester::#BR6] Test failed

And here’s a snippet of my code:

 private void handleDefault(String[] _args) {
        try {
            ArrayList<Process> processes = new ArrayList<>();
            boolean wc = false;

            for (ArrayList<String> currArgs: nextCommands) {
                String command = currArgs.get(0);
                if (command.trim().equals("wc"))    wc = true;
                if (!executables.containsKey(command)) {
                    handleOutput(command + ": not found\n\r");
                    return; 
                }

                currArgs.set(0, executables.get(command));
                ProcessBuilder pb = new ProcessBuilder(currArgs);
                Process process = pb.start();
                processes.add(process);
            }

            boolean finalWc = wc;
            ArrayList<Thread> outputThreads = new ArrayList<>();

            for (int i = 1; i < processes.size(); i++) {
                Process currProcess = processes.get(i);
                Process prevProcess = processes.get(i - 1);
                Thread pipe = new Thread(() -> {
                    try {
                        OutputStream currIn = currProcess.getOutputStream();
                        InputStream prevOut = prevProcess.getInputStream();
                        int byteRead;

                        while ((byteRead = prevOut.read()) != -1) {
                            try {
                                currIn.write(byteRead);
                                currIn.flush();
                            } catch (IOException e) {
                                break;
                            }
                        }

                        currIn.close();
                    } catch (IOException e) {
                        prevProcess.destroy();
                    }
                });

                Thread errorPipe = new Thread(() -> {
                    try {
                        InputStream errorIn = prevProcess.getErrorStream();
                        OutputStream errorOut = System.out;

                        int byteRead;
                        while ((byteRead = errorIn.read()) != -1) {
                            errorOut.write(byteRead);
                            errorOut.flush();
                        }
                    } catch (IOException e) {
                        System.out.println("\n\rError in process: " + e.getMessage());
                    }
                });

                pipe.start();
                errorPipe.start();
                outputThreads.add(pipe);
                outputThreads.add(errorPipe);
            }

            if (processes.size() > 0) {
                Thread outputStream = new Thread(() -> {
                    try {
                        Process last = processes.getLast();
                        InputStream in = last.getInputStream();
                        OutputStream out = System.out;

                        int byteRead;
                        if (finalWc) out.write(32);
                        while ((byteRead = in.read()) != -1) {
                            out.write(byteRead);
                            if (byteRead == '\n')   out.write('\r');
                            out.flush();
                        }
                        System.out.print("\r");
                    } catch (IOException e) {
                        System.out.println("\rError while executing process: " + e.getMessage());
                    } 
                });

                Thread errorOutputStream = new Thread(() -> {
                    try {
                        Process last = processes.getLast();
                        
                        int byteRead;
                        InputStream errorIn = last.getErrorStream();
                        OutputStream errorOut = System.out;

                        while ((byteRead = errorIn.read()) != -1) {
                            errorOut.write(byteRead);
                            errorOut.flush();
                        }
                    } catch (IOException e) {
                        System.out.println("\rError while executing process: " + e.getMessage());
                    } 
                });

                outputStream.start();
                errorOutputStream.start();
                outputThreads.add(outputStream);
                outputThreads.add(errorOutputStream);
            }

            for (Thread currThread: outputThreads) {
                currThread.join();
            }

            

            for (Process process: processes) {
                process.waitFor();
                if (process.isAlive()) {
                    process.destroy();

                    if (!process.waitFor(1, TimeUnit.SECONDS)) {
                        process.destroyForcibly();
                    }
                }
            }
        } catch (IOException | InterruptedException e) {
            System.out.println("\n\r" + e.getMessage());
        }
    }
1 Like

Hey @Shubh-81, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.