I have a log file on two different servers. The servers are under a load balancer so half the traffic goes to one server, and half the traffic goes to the other server.

I need to take the newest log file from one machine and transfer that log file to the other machine. So if one log file is changed on one server, it gets updated on the other server.

I think I need to use rsync. And do I also need to put it in a cron job?

  • It'll not work as you described... use remote syslog to one host or process the logfiles and merge them into one. – Kamil Šrot Nov 4 '12 at 18:28
  • should I do that using rsync? And a cron job? – user1704877 Nov 4 '12 at 18:32

rsync cannot merge the contents of two logfiles into one.

So long as you rsync the remote log into a different directory than the one that contains the local log, you will be able to make a synchronised copy so that one computer holds all log data (though in two files).

As suggested in a comment, to merge logfiles continuously, configure the server software to log through the syslog facility to a central syslog server.

  • I don't need to merge the two files, I need to take the newest file and copy it over to the other server. – user1704877 Nov 7 '12 at 17:18
  • @user1704877: In that case, use rsync in a cron job. – RedGrittyBrick Nov 7 '12 at 17:59

You can use the -u a.k.a. --update option to rsync and launch it automatically with crontab. From the rsync man page:

-u, --update                skip files that are newer on the receiver

Notice also the other options that may serve other needs:

 --inplace               update destination files in-place
 --append                append data onto shorter files
 --append-verify         --append w/old data in file checksum

As far as using a cron job, take a look at crontab. You give crontab a file that it reads to know when to launch tasks. The format of the file is:

minute hour day_of_month month day_of_week COMMAND

You can make a job execute every 7 minutes with a file like:

#  .---------------- minute (0 - 59) 
#  |   .------------- hour (0 - 23)
#  |   |   .---------- day of month (1 - 31)
#  |   |   |   .------- month (1 - 12) OR jan,feb,mar,apr ... 
#  |   |   |   |  .----- day of week (0 - 7) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat 
#  |   |   |   |  |
#  *   *   *   *  *  command
   */7 *   *   *  *  mail -s 'cron working' youremail@example.com

Assuming you name your crontab file as ~/crontab.bash, you install the crontab jobs on a particular machine like:

crontab ~/crontab.bash

See the wiki page for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.