2

I have a app server that I start and it writes output to stdout. I want to be able to watch that output and issue a command (in this case growlnotify -m "Server is up") when a specific line is sent to stdout, but I still want the all the output go to stdout.

Note I'd rather not write to a file

2
  • Note: I think (you should test this) on UNIX/Linux/MacOSX that if you decide at some point to kill the notifier app, since you're notified, you'd kill your server as well (unless you ignore SIGPIPE). So you'd need to keep it running or lose your server. Sep 10, 2012 at 22:14
  • in the case of growl it's just sending an async message to a bg process that shows the notification on screen. You don't generally kill it but I have tested with the accepted answer and dismissing the message works fine without killing the server.
    – dstarh
    Sep 11, 2012 at 2:41

1 Answer 1

2

Pipe the output of the server command to a while loop:

server | while read; do if [[ $REPLY =~ "Server is up" ]]; then
    # do something
  fi
  echo "$REPLY"
done
3
  • You can have the server running in the background, writing to a log file. Then tail -f logfile | while read; do ...; done, using the same while loop as in the answer.
    – chepner
    Sep 10, 2012 at 23:15
  • i always want the server in the foreground in this case so ctrl+c will stop it also don't want log files
    – dstarh
    Sep 11, 2012 at 2:34
  • And we have a winner! this worked perfectly.
    – dstarh
    Sep 11, 2012 at 2:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .