Skip to content

Instantly share code, notes, and snippets.

@crazy-max
Last active December 18, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy-max/5695501 to your computer and use it in GitHub Desktop.
Save crazy-max/5695501 to your computer and use it in GitHub Desktop.
Log all output
#! /bin/sh
LOG_FILE="/tmp/script-output.log"
function watchTail() {
local cur_pid=$$
local tail_args=`echo "tail -f $LOG_FILE" | cut -c1-79`
local pid=`ps -e -o pid,ppid,args | grep ${cur_pid} | grep "${tail_args}"| grep -v grep | nawk '{print $1}'`
if [ "$pid" = "" ]
then
if [ -z "$PS1" ]; then exit 0; else return 0; fi
fi
local ppid=2
while [ "$ppid" != "1" ]
do
local pids=`ps -e -o pid,ppid,args | grep "${tail_args}"| grep ${pid} | grep -v grep | nawk '{print $1"-"$2}'`
if [ "$pids" == "" ]; then break; fi
local ppid=`echo ${pids} | nawk -F- '{print $2}'`
if ((ppid==1))
then
sleep 3
kill -9 $pid
fi
done
}
# Output to log file
exec 1>"$LOG_FILE" 2>&1
# Starting to print log file on screen
term=`tty`
if [ -z "`echo $term | grep "/dev/"`" ]
then
term=""
tail -f "$LOG_FILE"
else
tail -f "$LOG_FILE">$term &
fi
# Starting watch in background and process
watchTail &
# Begin script
echo "Hello world!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment