An introduction to lsyncd

Hello squirrels,

Today we’re going to do a gentle introduction to lsyncd. lsyncd is a file synchronizer. Many folks confuse lsyncd with it’s cousin rsync. The difference is that rsync is a one-off solution (unless you automate it via cron etc.). lsyncd will use inotify to detect changes to files and will launch rsync for you. That’s right – lsyncd uses rsync.

Here’s how to set it up:

1. Get the lsyncd dependencies (one of them is rsync so we install the IUS repository to get the most recent version:

yum install -y
yum install -y rsync31u
yum install -y cmake lua-devel gcc gcc-c++ openssh-clients

2. Get the lsyncd source code – at the time of writing the latest version was 2.1.6:

cd /tmp
curl -LO
tar -xzf release-2.1.6.tar.gz
cd lsyncd-release-2.1.6/

3. Now compile lsyncd:

cmake .
make install

Also copy the manual page in place in case you need it:

cp doc/lsyncd.1 /usr/local/share/man/man1/

This will install lsyncd under /usr/local/bin.

4. Check the version:

lsyncd -version
Version: 2.1.6

5. Create an SSH key pair to use specifically for lsyncd (optional but good idea)

ssh-keygen -f ~/.ssh/squirrel5-lsyncd -N ''

6. Also add the key in your authorized_keys file so that you can still ssh in after the sync is done

cat /root/.ssh/ >> /root/.ssh/authorized_keys

7. Now add a config file under /usr/local/etc/lsyncd.conf:

In a previous post we showed how to use rsync to migrate CentOS from one machine to another. Let’s do the exact same thing using rsync – for reference here’s the rsync command we used:

time /usr/bin/rsync -aHAXv --delete-after --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/etc/sysconfig/network-scripts/*"} / root@

This is what the exact same thing would look like in lsyncd:

settings {
	log = all,
        logfile    = "/var/log/lsyncd.log",
        statusFile = "/tmp/lsyncd.status",
        pidfile    = "/var/run/",
        nodaemon   = false,

        exclude={ "dev/*", "proc/*", "sys/*","tmp/*","run/*","mnt/*"
                ,"media/*", "lost+found", "etc/sysconfig/network-scripts/*" },
        rsync = {
                compress = false,
                checksum = true,
                 _extra = {"-e", "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/squirrel5-lsyncd -l root"}

As you can see the above includes the excluded directories and SSH options such as which private key to use and which user to connect as.

8. Start lsyncd by pointing it to the configuration file and setting the -nodaemon option so we can see all of it’s output:

lsyncd /usr/local/etc/lsyncd.conf -nodaemon


If you get an error like this:

06:05:41 Error: Terminating since out of inotify watches.
Consider increasing /proc/sys/fs/inotify/max_user_watches

Check what the number is currently:

cat /proc/sys/fs/inotify/max_user_watches


sysctl fs.inotify.max_user_watches
fs.inotify.max_user_watches = 8192

set it to 15 million:

sysctl fs.inotify.max_user_watches=15000000

9. We’re going to control lsyncd using monit – install it with:

yum install -y monit
chkconfig monit on && chkconfig --list monit

10. Add a very simple configuration for lsyncd in /etc/monit.d/lsyncd:

check process lsyncd pidfile /var/run/
 start program = "/usr/local/bin/lsyncd /usr/local/etc/lsyncd.conf"
 stop program  = "/bin/kill `/bin/cat /var/run/`"

11. Start lsyncd:

monit start lsyncd

12. Monitor it’s status:

monit status lsyncd
The Monit daemon 5.14 uptime: 4m 

Process 'lsyncd'
  status                            Running
  monitoring status                 Monitored
  pid                               1929
  parent pid                        1
  uid                               0
  effective uid                     0
  gid                               0
  uptime                            1m 
  children                          2
  memory                            2.3 MB
  memory total                      6.1 MB
  memory percent                    0.2%
  memory percent total              0.6%
  cpu percent                       0.0%
  cpu percent total                 0.0%
  data collected                    Sat, 03 Dec 2016 20:05:27


If the output you get looks like:

monit status lsyncd
The Monit daemon 5.14 uptime: 2m 

Process 'lsyncd'
  status                            Initializing
  monitoring status                 Initializing
  data collected                    Sat, 03 Dec 2016 20:01:08

Don’t worry about it – give monit a couple more minutes and it will get the status 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *