s6: scan directories
Description: s6: scan directory
Keywords: s6 scandir supervision svscan s6-svscan scan directory servicedir

s6
Software
skarnet.org

# Scan directories

A scan directory is a directory containing a list of service directories, or symbolic links pointing to service directories.

A scan directory represents a list of services that are supposed to be supervised. Running s6-svscan on this scan directory launches a supervision tree: every service listed in the scan directory will be supervised.

There is normally only one scan directory per system, although nothing prevents a system administrator from having more. daemontools traditionally uses `/service`, and runit traditionally uses `/etc/service`. s6 does not care where your scan directory is, but I would advise `/service` for compatibility with daemontools. Depending on your installation, `/service` could be a symbolic link and point to a place either in a RAM filesystem or in `/var`.

## Where and how to build a scan directory

Opinions and practices differ.

It is generally accepted that the place where you store all your service directories (your "service repository") should not be used as a scan directory - for a simple reason: you might want to have service directories for more services than what you want to start at any given time. In other words, your scan directory will be a subset of your service repository, so you cannot just run s6-svscan on every service you have a service directory for. So, the first thing is to separate your service repository, which is just a storage place for all the services you might want to manage someday, and your scan directory, which is a directory representing all the services that you are currently managing.

### Service repository

Where to store your service repository is purely a matter of personal preference. You just have to be aware that s6-supervise needs writable `supervise` and `event` subdirectories in a service directory it monitors.

### Scan directory

Where and how to build your scan directory depends heavily on your boot system - and on your personal preference too.

Standard daemontools and runit installations like to have a fixed scan directory containing symlinks to service directories located in the service repository. In other words, the service repository contains the real working copies of the service directories. This works, as long as:

- It is possible to create writable `supervise` and `event` subdirectories in every managed service directory. This can be achieved for instance via symlinks, or by having the service repository stored on a writable filesystem.
- The scan program (s6-svscan, svscan, runsvdir...) is started late enough for all the necessary filesystems to be mounted.

My own recommendation would be to have working copies of the service directories entirely separate from the service repository. The service repository can be safely stored on the root filesystem, and the needed directories copied to a RAM filesystem at boot time. The scan directory can be either the place where the working copies are written, or another directory containing symlinks to those working copies. (The latter is useful if you are using the `-t` option to s6-svscan with a nonzero argument: copying a directory is not atomic, but making a symlink is, so there is no risk of your scanner finding a directory while it is being copied - which could result in s6-supervise getting the wrong information and not managing the service properly.)

An example:

- Have your service repository in `/img/services`, i.e. have service directories in `/img/services/ftpd`, `/img/services/httpd`, `/img/services/sshd`, etc.
- When booting, make `/tmp` a RAM filesystem, and create the directories `/tmp/services` and `/tmp/service`.
- Have s6-svscan run on `/tmp/service`, as early as possible in your boot sequence. This is possible whether you want to run s6-svscan as process 1 or not.
- During the boot sequence, populate `/tmp/services` with copies of the service directories you need: for instance,
  - `cp -a /img/services/sshd /tmp/services/sshd`
  - `cp -a /img/services/ftpd /tmp/services/ftpd`
  - etc.
- When you are ready to start a service, make a symlink in the `/tmp/service` scan directory pointing to the working copy of the service directory you need in `/tmp/services`, then notify s6-svscan. For instance, to start ftpd and httpd together:

       ln -s ../services/ftpd /tmp/service
       ln -s ../services/httpd /tmp/service
       s6-svscanctl -a /tmp/service