You are not logged in.

#1 2014-08-07 18:42:44

jernst
Member
From: Silicon Valley
Registered: 2014-03-04
Posts: 296
Website

systemd service dependencies

Assume I have a food (the "foo" daemon -- couldn't resist) that occasionally requires access to a running mysql database server.

Can I express this somehow using systemd, so that when I say:

    > systemctl start food

systemd will check first whether mysqld is running, and if not, start it?

(This is not about food or mysqld, it's a question about systemd.)

A more complex version of the same question could be bard (the bar daemon) depending on a locally running mail server, without specifying which particular one.

Thanks,


Johannes.

Offline

#2 2014-08-07 20:50:21

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: systemd service dependencies

Isn't this explained in the wiki page and man pages?

Offline

#3 2014-08-07 21:02:34

jernst
Member
From: Silicon Valley
Registered: 2014-03-04
Posts: 296
Website

Re: systemd service dependencies

Perhaps I'm not seeing the trees because of all the forest. Any chance you could point me to an example?

Offline

#4 2014-08-07 21:10:01

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: systemd service dependencies

man systemd.unit

Although your requirements are quite ambiguous: "occasionally requires" and "depending on which one" muddy this issue somewhat. I suspect you are looking for Wants.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2014-08-07 21:37:13

jernst
Member
From: Silicon Valley
Registered: 2014-03-04
Posts: 296
Website

Re: systemd service dependencies

Ah! Thank you. I never reached that man page.

So I would have food.service Requires or Wants or BindsTo mysqld.service, and possibly also an After, so I'm sure that by the time food arrives, mysqld is ready. Great!

Add-on: now let's assume that food requires a particular MySQL configuration setting (like, say, binary logs being on). Manually, I would do this:
1. Edit MySQL config file, e.g. using perl -pi, so that the setting is on
2. If MySQL is not running, start it
3. If MySQL is running, restart it
4. Start my actual daemon

So the Wants and Requires and all that lets me elegantly deal with the case where MySQL isn't running yet. But what if it is, and MySQL needs to be restarted? Does systemd somehow support that use case as well, and I (again) didn't find the documentation for it? (If so, I'm very sorry and appreciate all pointers)

Side note: Systemd does not seem to like a "systemd restart xyz" invocation from within another systemd start invocation, so I cannot write the above four steps into a food.service script. (Apparently the "inner" systemctl invocation waits for the previous one to finish before proceeding, i.e. deadlock)

Offline

#6 2014-08-07 22:25:09

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,564
Website

Re: systemd service dependencies

The "Wants" entries name other services (e.g. other systemd service files) not programs.  So you could have a mysql-bin-logs.service and a mysql-no-logs.service (which should then conflict with each other).  Food.service would just specify that it wants mysql-bin-logs.

EDIT: in fact you'd probably want to use variables rather than the two different service files - look at dhcpcd@.service for a common example.  You could then have mysql@logs or mysql@nologs or something of the sort.

Last edited by Trilby (2014-08-07 22:26:46)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2014-08-07 22:31:08

jernst
Member
From: Silicon Valley
Registered: 2014-03-04
Posts: 296
Website

Re: systemd service dependencies

@trilby: that is so much cleaner! I like it. Thank you.

Offline

Board footer

Powered by FluxBB