2

I'm trying to refactor a single systemd service/program into multiple programs and services. My single big service looked like:

[Unit]
Description=Runs the App loop
After=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 -u /App/mainLoop
WorkingDirectory=/App
StandardOutput=journal
Restart=on-failure

[Install]
WantedBy=multi-user.target

I cobbled this together from other examples. What I want now is two services:

ServiceA

[Unit]
Description=Initializes some things
After=network-online.target # Question 1

[Service]
Type=simple
ExecStart=/usr/bin/python3 -u /App/startup
WorkingDirectory=/App
StandardOutput=journal
Restart=on-failure # Question 2

[Install]
WantedBy=multi-user.target

ServiceB

[Unit]
Description=Runs the App loop
After=network-online.target # Question 3

[Service]
Type=simple
ExecStart=/usr/bin/python3 -u /App/mainLoop
WorkingDirectory=/App
StandardOutput=journal
Restart=on-failure

[Install]
WantedBy=multi-user.target # Question 4

So I have multiple question points here (referenced by the comments above):

  1. I don't need the network to be alive anymore for this. What should I put here instead? How does one browse possible targets?
  2. I want the startup app to just run once at startup, never again. I assume I need to change the Restart flag to something else, but what?
  3. Rather than after the network of course, I want this to run after service A completes AND ONLY IF it managed to exit with a 0 and write a file. How do I make it start after the one time service A starts successfully?
  4. More of 3 I guess. I'm still fuzzy on what the difference is between the WantedBy target and the After target. Is the WantedBy basically the group it belongs to, and the After the one that it has to be serialized after?
5
  1. The targets depend on OS, which you haven't listed here. Probably default.target. You can view targets and what uses by looking at the output of systemctl list-dependencies
  2. Just remove "Restart" entirely
  3. The initial service should be Type=oneshot. It will be up to the service to properly exit based on whether or not it wrote the file you require. You may want to use ExecStartPre to launch initialization scripts instead.
  4. WantedBy tells systemd to start the service when it enters the given target. After says to start the unit after all units in the given target (or unit) have completed.
  • 1
    I'm running Debian Jessie on a headless SBC. I put default.target as you suggested, but it doesn't run. find / | grep default.target actually shows 2 such files. A symlink at /lib/systemd/system/default.target -> graphical.target and another normal file at /usr/lib/systemd/user/default.target which includes things like After=basic.target. Not sure what to make of all of that. :( – Travis Griggs Jan 15 '16 at 22:08
  • Shouldn't #3 be Requires: ..., unless Service A is made a Type=oneshot? Otherwise, it starts it after but not once Service A is considered ready... Correct? – Alexis Wilke Aug 21 '16 at 23:36
  • About #1: I think the line can just be removed if network is not required anymore. – JojOatXGME Jun 1 '17 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.