Operating System - HP-UX
1752794 Members
6136 Online
108789 Solutions
New Discussion юеВ

Re: Bash script for "tails -f"

 
SOLVED
Go to solution
RiclyLeRoy
Frequent Advisor

Bash script for "tails -f"

My HP-UX B.11.00 server produces 3 log files (t1,t2,t3) which I want to send to syslod.
I tried to run a specific script for executing this command and it works fine :
tail -f t1 | logger &

But how I also can send "tail -f" of t2,t3 to syslogd into same command ? Because I wish having only one process to kill and not 3 processes for 3 tails. Suggestions ?!


8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: Bash script for "tails -f"

Hi:

You could launch one script as a background process that looks something like this:

# cat ./mytails
#!/usr/bin/sh
tail -f t1 | logger &
PID1=$!; PIDS=${PID1}
tail -f t2 | logger &
PID2=$!; PIDS=$(echo ${PIDS} ${PID2})
tail -f t3 | logger &
PID3=$!; PIDS=$(echo ${PIDS} ${PID3})
echo "${PIDS}" > /tmp/mytails.pids
wait ${PID1}; wait ${PID2}; wait ${PID3}
exit 0

# ./mytails &

Now, when you want to terminate all processes do:

# kill $(
Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Bash script for "tails -f"

Hi (again):

I neglected to add cleanup for the temporary file. This corrects that:

# cat ./mytails
#!/usr/bin/sh
FILE=/tmp/mytails.pids
trap 'rm -f ${FILE}' EXIT
tail -f t1 | logger &
PID1=$!; PIDS=${PID1}
tail -f t2 | logger &
PID2=$!; PIDS=$(echo ${PIDS} ${PID2})
tail -f t3 | logger &
PID3=$!; PIDS=$(echo ${PIDS} ${PID3})
echo "${PIDS}" > ${FILE}
wait ${PID1}; wait ${PID2}; wait ${PID3}
exit 0

Regards!

...JRF...
Wouter Jagers
Honored Contributor

Re: Bash script for "tails -f"

or possibly use a little script instead of tail.. the below seems to work, but I don't know how good an idea it would be to use it on -really- active logfiles. Consider it my quick and dirty friday night excercise ;-)

This assumes a file ./logfiles exists, in which the files to watch are listed (t1 t2 t3). Every 5 seconds it checks if the no. of lines has increased. If so, the new lines are logged. If a file suddenly has less lines, a log reset is assumed and the file is logged from line 1.

It's one job to launch (or kill), you can tweak the intervals (and pretty much anything, of course), and if a file gets reset with content ( echo "resetting log" > t1 ) it doesn't seem to miss the first line as tail tends to do.

Again, I have not truly tested this or anything, so please do and improve where needed, but who knows it could be useful.

Cheers
Wout


#!/bin/sh
FIRST=1
while true
do
if [ $FIRST -eq 1 ]
then
for i in $( do
eval ${i}C=$(wc -l $i|awk '{print $1}')
done
FIRST=0
else
for i in $( do
eval ${i}N=$(wc -l $i|awk '{print $1}')
eval NN=\$${i}N
eval CC=\$${i}C
if [ $NN -gt $CC ]
then
eval ${i}C=$((${i}C+1))
eval sed -n "\$${i}C,\${${i}N}p" $i | logger
eval ${i}C=\$${i}N
elif [ $NN -lt $CC ]
then
echo "$i decreased in size, assuming reset." | logger
eval sed -n "1,\${${i}N}p" $i | logger
eval ${i}C=\$${i}N
fi
done
fi
sleep 3
done
an engineer's aim in a discussion is not to persuade, but to clarify.
Wouter Jagers
Honored Contributor

Re: Bash script for "tails -f"

5 seconds, 3 seconds.. let's say I was undecided ;-)

an engineer's aim in a discussion is not to persuade, but to clarify.
RiclyLeRoy
Frequent Advisor

Re: Bash script for "tails -f"

Hi Wouter Jagers, what advantage can I having to create new script instead to run tail command ?
Wouter Jagers
Honored Contributor

Re: Bash script for "tails -f"

You wanted to have only one process to kill. This one you can launch in the background and would be one job.

I don't know what the perks or drawbacks would be vs tail, you'd have to test.

I did notice that when I reset a file by echoing a line ( echo reset > t1 ), tail tends to drop that first line. This thing logs the complete file when its size decreased, so you shouldn't miss any lines when that happens. You could also edit it further to fit your needs.

Cheers
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
James R. Ferguson
Acclaimed Contributor

Re: Bash script for "tails -f"

Hi (again):

Here's a much cleaner version with a consistent approach and the absence of the need for any temporary file handling.

# cat ./mytails
#!/usr/bin/sh
trap 'kill ${PID1} ${PID2} ${PID3}' HUP INT QUIT TERM
tail -f t1 | logger &
PID1=$!
tail -f t2 | logger &
PID2=$!
tail -f t3 | logger &
PID3=$!
wait ${PID1}; wait ${PID2}; wait ${PID3}
exit 0

Normally you would want to launch this into the background:

# ./mytails &

Now, to terminate, simply 'kill' the process in the background ('kill %1' for example) or use the pid of the 'mytails' process:

If you start the 'mytails' in the foreground, a simple 'control_C' will also will terminate everything too.

If you start 'mytails' as below, you can logoff your sesssion, of course:

# nonhup ./mytails &

...and then latter easily find and kill everything with:

# UNIX95= ps -C mytails -o pid=|xargs kill

Notice that a space (whitespace) follows 'UNIX95=' without an intervening semicolon before the 'ps' command.

Regards!

...JRF...

RiclyLeRoy
Frequent Advisor

Re: Bash script for "tails -f"

good suggestions to create script