Search code examples
centos7systemdgraphiterhel7

How do I run multiple carbon services in one systemd file?


Currently, I run the following bash script when starting my carbon instances for graphite:

carbon_cache_count=8
carbon_relay_count=1
carbon_aggr_count=1

for i in `seq ${carbon_relay_count}`; do sudo python /opt/graphite \
/bin/carbon-relay.py --instance=${i} start; done
for i in `seq ${carbon_cache_count}`; do sudo python /opt/graphite \
/bin/carbon-cache.py --instance=${i} start; done
for i in `seq ${carbon_aggr_count}`; do sudo python /opt/graphite \
/bin/carbon-aggregator.py --instance=${i} start; done

How would I do the same thing using systemd? Do I need to clarify each carbon instance with ExecStart, or is there a way to iterate through a loop value to load them all in one ExecStart line, and how could I do that?

I've seen an example that uses:

ExecStart=/opt/graphite/bin/carbon-cache.py --instance=%i start

But not sure where %i is defined, or if that will also reach the same result as my bash script. Any thoughts, suggestions, and sources would be greatly appreciated.


Solution

  • For systemd you can use instances.

    See the unit files from the epel7 rpm package for example:

    https://src.fedoraproject.org/rpms/python-carbon/blob/epel7/f/carbon-cache%40.service

    To start you just run:

    systemctl start 'carbon-cache@1'

    For more information on systemd instances:

    http://0pointer.de/blog/projects/instances.html