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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:52:15+00:00 2026-05-30T10:52:15+00:00

I want to process each stdout-line for a shell, the moment it is created.

  • 0

I want to process each stdout-line for a shell, the moment it is created. I want to grab the output of test.sh (a long process). My current approach is this:

 ./test.sh >tmp.txt &
 PID=$!
 tail -f tmp.txt | while read line;  do
 echo $line
 ps ${PID} > /dev/null
 if [ $? -ne 0 ]; then
     echo "exiting.."
 fi
 done;

But unfortunately, this will print “exiting” and then wait, as the tail -f is still running. I tried both break and exit

I run this on FreeBSD, so I cannot use the --pid= option of some linux tails.

I can use ps and grep to get the pid of the tail and kill it, but thats seems very ugly to me.

Any hints?

  • 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-30T10:52:17+00:00Added an answer on May 30, 2026 at 10:52 am

    why do you need the tail process?

    Could you instead do something along the lines of

    ./test.sh | while read line; do
      # process $line
    done
    

    or, if you want to keep the output in tmp.txt :

    ./test.sh | tee tmp.txt | while read line; do
      # process $line
    done
    

    If you still want to use an intermediate tail -f process, maybe you could use a named pipe (fifo) instead of a regular pipe, to allow detaching the tail process and getting its pid:

    ./test.sh >tmp.txt &
    PID=$!
    
    mkfifo tmp.fifo
    tail -f tmp.txt >tmp.fifo &
    PID_OF_TAIL=$!
    while read line; do
      # process $line
      kill -0 ${PID} >/dev/null || kill ${PID_OF_TAIL}
    done <tmp.fifo
    rm tmp.fifo
    

    I should however mention that such a solution presents several heavy problems of race conditions :

    • the PID of test.sh could be reused by another process;
    • if the test.sh process is still alive when you read the last line, you won’t have any other occasion to detect its death afterwards and your loop will hang.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a directory logfiles. I want to process each file inside this directory
I want to read line-by-line from a given input file,, process each line (i.e.
I want to process each source code file after it has been preprocessed: myprocess
I need execute some code before Windows shutdown process each time. So, I want
I want to process every element of a for-loop. Taking this code, why is
I want to know the following information: For each process on the windows platform:
I want to process images to establish say, 9 areas inside each one, then
I want to know if the global descriptor table resides in each process memory
I want to process a medium to large number of text snippets using a
I have the following XML and I want to process it so that I

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.