#!/bin/bash ### Serve a log realtime from a port with netcat ### # Copyright 2009 Ertug Karamatli # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . if [ -z "$1" ]; then echo Usage: $0 LOGFILE [PORT] exit -1 fi if [ -n "$2" ]; then PORT=$2 else PORT=9977 fi TAIL_CMD="tail -f $1" function kill_tail { # find and kill the tail process that is detached from the current process TAIL_PID=$(/bin/ps -eo pid,args | grep "$TAIL_CMD" | grep -v grep | awk '{ print $1 }') kill $TAIL_PID } trap "kill_tail; exit 0" SIGINT SIGTERM while true; do ( $TAIL_CMD & ) | nc -l -p $PORT -vvv kill_tail done