News for hackers. Stuff that matters.
Re:Taming non-terminating Bash processes by fensipens on Saturday November 08, 2014 @03:32PM (#8578072)
Sorry Pädu but this smells for many reasons.. first:

     The dns-sd command is primarily intended for interactive use.  Because
     its command-line arguments and output format are subject to change,
     invoking it from a shell script will generally be fragile. Additionally,
     the asynchronous nature of DNS Service Discovery does not lend itself
     easily to script-oriented programming. For example, calls like "browse"
     never complete;
Second, "SIGKILL is too verbose, SIGINT is too soft".. how about SIGTERM? Its very likely that you're doing it wrong if you need to use -KILL in your scripts.

Third, "[1] 58181 killed ./discover-vnc.sh --- It's crucial to suppress this line." again, if you think you must suppress this, you're doing it wrong. This shouldn't pop up in the first place.

Fourth, what about fifos and `$!'?

Fifth, why bash? Use the sh-subset.

etc.

It's cute that you managed to solve your problem but this is nothing more than an unreliable hack, pompously presented ("Taming non-terminating Bash processes", "Kill the children"..)

Re:Taming non-terminating Bash processes by pstadler on Saturday November 08, 2014 @04:02PM (#8578139)
Thanks man, your feedback is appreciated. I'm well aware of the dirty nature of this solution[1] but it seems to be the only way to achieve this within context (OS X, Alfred).

[1] https://github.com/pstadler/non-terminating-bash-processes#c...

Edit: $! is not working with the distributed Bash version (currently 3.2) on OS X. Unfortunately, dns-sd is the only utility to discover network services on OS X. There are many projects which use the same hack involving dns-sd.

I invite you to find a better solution to this problem.

Re:Taming non-terminating Bash processes by fensipens on Saturday November 08, 2014 @04:38PM (#8578223)
Try something like this (untested):

  #! /bin/sh

  fifo=/tmp/discover-vnc.fifo
  rm -f $fifo
  mkfifo $fifo

  dns-sd -B _rfb._tcp >$fifo &
  pid=$!

  while read line; do
    case $line in *_rfb._tcp.*)
      # do something here.. then test
      # if flag=3 and break otherwise
    esac
  done <$fifo

  kill $pid
Re:Taming non-terminating Bash processes by pstadler on Saturday November 08, 2014 @05:12PM (#8578301)
Results from dns-sd are not necessarily being delivered instantly and there might be no lines which contain this flag to check of it's the last entry. It's also rather ugly to create a named pipe for this purpose, don't you think?
Re:Taming non-terminating Bash processes by fensipens on Saturday November 08, 2014 @06:06PM (#8578439)
there might be no lines which contain this flag

Again, untested:

        #! /bin/sh

        timeout=5
        fifo=/tmp/discover-vnc.fifo
        rm -f $fifo
        mkfifo $fifo

        dns-sd -B _rfb._tcp >$fifo &
        pid1=$!

        while read line; do
          case $line in *_rfb._tcp.*)
            # do something here.. then test
            # if flag=3 and break otherwise
          esac
        done <$fifo &
        pid2=$!

        sleep $timeout && kill $pid1 $pid2 &
        pid3=$!

        wait $pid2
        kill $pid3
It's also rather ugly to create a named pipe for this purpose

Because it creates a file in /tmp? Who cares? The alternative is bash voodoo magic.. the same magic that gave us shellshock.

Re:Taming non-terminating Bash processes by pstadler on Saturday November 08, 2014 @07:05PM (#8578592)
The alternative is bash voodoo magic.. the same magic that gave us shellshock.

I love this one.

Re:Taming non-terminating Bash processes by fensipens on Saturday November 08, 2014 @04:56PM (#8578266)
$! is not working with the distributed Bash version (currently 3.2) on OS X.

  $ bash --version
  GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
  Copyright (C) 2007 Free Software Foundation, Inc.
  $ : & echo $!
  [1] 329
  329
Re:Taming non-terminating Bash processes by pstadler on Saturday November 08, 2014 @05:06PM (#8578293)
$BASHPID is not working with subshells prior to Bash v4.
Re:Taming non-terminating Bash processes by fensipens on Saturday November 08, 2014 @05:32PM (#8578340)
$BASHPID and $! are two different things.
Re:Taming non-terminating Bash processes by pstadler on Saturday November 08, 2014 @06:06PM (#8578437)
I need to know the PID of the subshell, inside the subshell. $BASHPID was introduced to hold this value. While $$ still contains the PID of the parent process and $! is not available inside the subshell after all.
Re:Taming non-terminating Bash processes by ttwwmm on Saturday November 08, 2014 @04:24PM (#8578193)
The use of <(..) is really weird here... why not just pipe dns-sd to the while loop?

    dns-sd | while read -r line ; do
        ...
    done
Sure, you won't be able to accumulate the output for the trap, but you shouldn't need to---this is stream processing!

Consider replacing the while loop with awk. This sort of thing is exactly what awk is good at:

    BEGIN { FS = " "; count = 0; }
    NR > 5 {
        count++
        print
        if ($3 != "3") { exit; }
    }
    END { printf("%d hosts found\n", count); }
You could also do it with GNU sed (you'll need the q or Q command for early exit, and that's a GNU extension---no idea if it's available on OS X).
Re:Taming non-terminating Bash processes by pstadler on Saturday November 08, 2014 @04:29PM (#8578202)
This will stay in the loop if there's no output or if no flag == 3 is received.
Re:Taming non-terminating Bash processes by fragmede on Saturday November 08, 2014 @03:31PM (#8578068)
That's all well and good, but the real issue is that dns-sd is really meant to be used from a shell script.

It should be noted that Linux's command line program to enumerate network services, avahi-browse, includes a '-t' flag so that terminates after dumping service entries.

https://developer.apple.com/library/mac/documentation/Darwin...

Re:Taming non-terminating Bash processes by duskwuff on Saturday November 08, 2014 @10:32PM (#8579008)
ITYM "really not meant to be used from a shell script".