Linux – way to control two instantiated systemd services as a single unit

fedoralinuxsystemd

I've got a couple python web services I'm trying to run on a Fedora 15 box. They're being run by paster, and the only difference in starting them is the config file they read.

This seems like a good fit for systemd's instantiated services, but I'd like to be able to control them as a single unit. A systemd target that requires both services seems like the way to approach that. Starting the target does start both services, but stopping the target leaves them running.

Here's the service file:

[Unit]
Description=AUI Instance on Port %i
After=syslog.target

[Service]
WorkingDirectory=/usr/local/share/aui
ExecStart=/opt/cogo/bin/paster serve --log-file=/var/log/aui/%i deploy-%i.ini        
Restart=always
RestartSec=2
User=aui
Group=aui

[Install]
WantedBy=multi-user.target

And here's the target file:

[Unit]                    
Description=AUI           
Requires=aui@5001.service
Requires=aui@5002.service
After=syslog.target   

[Install]                 
WantedBy=multi-user.target

Is this kind of grouping even possible with systemd?

Best Answer

Look at using the BindsTo option to tie the units together. The systemd.unit(5) manual page describes it like this:

BindsTo=

Configures requirement dependencies, very similar in style to Requires=, however in addition to this behavior, it also declares that this unit is stopped when any of the units listed suddenly disappears. Units can suddenly, unexpectedly disappear if a service terminates on its own choice, a device is unplugged or a mount point unmounted without involvement of systemd.

Which sounds like it should do what you want and tie one unit to the other so that whenever the master stops the slave stops as well and vice versa for starting up.

Don't forget that you can also use Before/After to specify the order in which they should start if necessary.