Breaking "while read" also breaks the parent process

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
# 1  
Question Breaking "while read" also breaks the parent process


I'm a bit confused. Maybe some master can explain to me what is happening.

I have a program that starts issuing output about himself loading.
I want to run it in another thread but I want to wait untill it's fully loaded.

Program sample:
$ cat myprogram
echo "loading"
echo "almost ready"
echo "up and running"
while true; do
    echo "I don't care about the rest of the output"
    sleep 1

If I run it like this, it will stay in another thread but I will not be sure it is "up and running":
$ myprogram &

So I tried to run it like this but the process dies very soon :
$ myprogram | while read -r; do
    [[ "$REPLY" == "up and running" ]] && break
echo "Now I know myprogram went all the way untill it is up and running"
echo "But now myprogram is dead :-("

Question: How can I stat myprogram and wait untill it outputs "up and running"?

Thanks for your help.
# 2  
The reason your program is breaking is that the break will exit the loop, ending the read, which sends a SIGPIPE to myprogram, which usually is an exit condition.

Do you have influence on the code of the program being started? If so, have it create a file once it's done, for which you can wait.

If not you'll have to put the reading loop into the background, and have it signal the main program once it's done. Something like


trap waiting=0 USR1

  ./myprogram | while read -r
    [[ "$REPLY" == "up and running" ]] && kill -USR1 $ppid
) &

printf "Waiting for startup"
while [ $waiting -eq 1 ]
  printf '.'
  sleep 1

: # Stuff to be done here

wait #Wait for "myprogram" to exit

# 3  
Thanks pludi, I love your answer which is fairly new to me.
So basically you do:
trap waiting=0 USR1

Which I guess means: "waiting becomes 0 as soon as the signal USR1 is received"
And then you do:
kill -USR1 $ppid

Which I guess means: "Send signal USR1 to the process"
Can you confirm?
I read through man kill and I still have one question?
Are you strictly limited to the list of signals mentioned in the man page?
What makes you choose USR1?
I guess you need a safe signal that would not be easily used by other processes.

Thanks for your help
# 4  
Yes, that's about correct.

As for the signals, one is pretty much limited to the signals reported by "kill -l", minus a few. For example, SIGKILL can't be trapped. And for shell scripts there's the additional signal 0, which is triggered whenever the script exits (great for cleanup).

USR1 and USR2 are intended for use for IPC, as they have no fixed meaning. Other typical uses are SIGHUP (HangUP) to tell daemons to reinitialize themselves without restarting, SIGINT for catching ^C, or SIGWINCH when the terminal window was resized.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #817
Difficulty: Medium
In CSS, E[foo="bar"] matches an E element whose "foo" attribute value is exactly equal to "bar".
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

2. Shell Programming and Scripting

While loop breaking when using "ssh" command inside

Hi , I am trying to read a list of hosts from a config file and trying to get file list from that host. For this I have used one while loop. cat "$ARCHIVE_CFG_FILE" | sed '/^$/d' | sed '/^#/d' | while read ARCHIVE_CFG do SOURCE_SERVER_NAME=`echo "$ARCHIVE_CFG" | awk -F '|' '{ print... (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

3. Shell Programming and Scripting

Read from "list1" and list matches in "list2"

I want to print any matching IP addresse in List1 with List 2; List 1 List of IP addresses; (3 Replies)
Discussion started by: lewk
3 Replies

4. Shell Programming and Scripting

Purpose of "read" and "$END$" in ksh ?

Hi, Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator? cat $AMS/version|read a b verno d DBVer=$(/usr/bin/printf "%7s" $verno) I checked that the cat $AMS/version command returns following output: ... (10 Replies)
Discussion started by: dbadmin100
10 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Red Hat

"service" , "process" and " daemon" ?

Friends , Anybody plz tell me what is the basic difference between "service" , "process" and " daemon" ? Waiting for kind reply .. .. (1 Reply)
Discussion started by: shipon_97
1 Replies

7. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

8. Shell Programming and Scripting

Breaking input with "read" command

In this post, Perderabo's script says echo 05/06/25 14:15:56 | IFS=" /:" read Y1 M1 D1 h1 m1 s1 which, if I am not wrong, will break the input into Y1, M1 et al. I tried the following in my code #! /bin/ksh # typeset -R2 HOUR=00 typeset -R2 MIN=00 typeset -R2 SEC=00 ... (2 Replies)
Discussion started by: vino
2 Replies

9. Shell Programming and Scripting

how to request a "read" or "delivered" receipt for mails

Dears, I've written a script which allows me to send mails in different formats with different attaches. Now I still want to add a feature to this script. My users would like to be able to receive a "read" or "delivered" receipt for their mails. The script send mails on behalve of an specific... (1 Reply)
Discussion started by: plelie2
1 Replies

Featured Tech Videos