Re: problem with tail -f that need to add to each line constant string at the beginning of each line

2011-10-30 Thread dan sas
Hi

what about if i want to run both commands simultaneous and send the output
to a file
how can i do that



2011/10/29 Bob Proulx 

> dan12341234 wrote:
> > im running 4 scripts that perform ssh and tail -f on a file exist on 4
> > servers and display on the screen.
> > (1 script per servers)
> >
> > ssh server1 tail -f /var/log/apache/access_log
> > ssh server2 tail -f /var/log/apache/access_log
> > ssh server3 tail -f /var/log/apache/access_log
> > ssh server4 tail -f /var/log/apache/access_log
> >
> > the problem is that the display dosent show any identification from which
> > server each line
> >
> > what i need is something with an echo servername, before each line
> printed
> > to the screen
>
> There are many specialized programs to do exactly what you are asking
> such as dsh (distributed shell) and other tools.  You might want to
> investigate those.
>
> But you can do what you want by using sed to prepend to the line.
>
>  ssh -n server1 'tail -f /var/log/syslog | sed --unbuffered
> "s/^/$(hostname): /"'
>  ssh -n server2 'tail -f /var/log/syslog | sed --unbuffered
> "s/^/$(hostname): /"'
>
> You might consider applying the commands as standard input instead.  I
> think it is easier to avoid quoting problems that way.
>
>  echo 'tail -f /var/log/syslog | sed --unbuffered "s/^/$(hostname): /"' |
> ssh -T server1
>
> Bob
>


Re: problem with tail -f that need to add to each line constant string at the beginning of each line

2011-10-30 Thread Bob Proulx
dan sas wrote:
> what about if i want to run both commands simultaneous and send the
> output to a file how can i do that

This is getting further off topic for the bug bash mailing list.  It
really isn't too much about bash but just general help with how things
work.  A better place to ask these types of questions would be the
help-gnu-ut...@gnu.org mailing list.  If you have more questions in
the future let's please have the discussion there.

You could put the commands in the background.  To put commands in the
background use the '&' character at the end of the command line.  

   If  a  command  is terminated by the control operator &, the shell exe-
   cutes the command in the background in a subshell.  The shell does  not
   wait  for  the command to finish, and the return status is 0.  Commands
   separated by a ; are executed sequentially; the shell  waits  for  each
   command  to terminate in turn.  The return status is the exit status of
   the last command executed.

And also redirect the output to a file.

  ssh -n server1 'tail -f /var/log/syslog | sed --unbuffered "s/^/$(hostname): 
/"' >>syslog.tail.out &
  ssh -n server2 'tail -f /var/log/syslog | sed --unbuffered "s/^/$(hostname): 
/"' >>syslog.tail.out &

By this point it looks very much like you are trying to re-invent the
remote logging feature of a syslogd.  Because I now expect a new
question about how to start and restart this connection automatically.
(Am I right? :-)  Instead please investigate using one of the syslog
programs that does exactly that already.  See for example 'rsyslogd'
which supports remote logging.

Good luck!
Bob