16

After searching plenty through plenty a post, Youtube video, and "documentation" on the matter of systemd, I'm still at a loss.

The link (https://wiki.archlinux.org/index.php/systemd#Create_custom_target) seemed promising, but was a bit vague (to me).

Question

How would one go about creating a custom systemd target (IE: foo.target ) so that one may boot with select .service units?

Example

  1. System boots default.target (symlink of "foo.target")
  2. "foo.target" only starts a barebones X server and GUI program, say "gvim".

Reason

I'm simply looking to create a custom target for quickly launching one X program. I'd be nice to exclude all the services I don't need.

Thanks in advance!

16

Reading through man 5 systemd.unit and man 5 systemd.target tells us that unit files are used to define targets as well as everything else systemd. There is no documentation specifically on how to create a target, so it's hard to determine the how it should be done, but it is not too different from creating a service.

When you create your target, you will need to make symlinks to the target.wants directory from the systemd services directory. Then you can set/boot your target. Here's how it might look given your example.

/etc/systemd/system/foo.target

This is the target's unit file. If graphical.target is taken as an example, we can create our own target using it as a base.

[Unit]
Description=Foobar boot target
Requires=multi-user.target
Wants=foobar.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target
AllowIsolate=yes

To explain the options taken from the systemd manpages;

  • Description -- Describes the target. You should understand
  • Requires -- Hard dependencies of the target. You should let the basic system start before you start your own service(s)
  • Wants -- Soft dependencies. The target does not require these to start.
  • Conflicts -- If a unit has a Conflicts setting on another unit, starting the former will stop the latter and vice versa.
  • After -- Boots after these services
  • AllowIsolate -- Really up to you and your environment. Details are available in the manpage systemd.unit(5)

/etc/systemd/system/foo.target.wants/

This is the directory where you will link the services you create/require for your target. It is equivalent to the Wants= option in the unit file. Create this directory and then create symlinks like so; ln -s /usr/lib/systemd/system/bar.service /etc/systemd/system/foo.target.wants/bar.service. This creates a symlink from bar.service in the system directory to your foo.target.wants directory.


I think creating a unit file for a service is kind of out of the scope of this answer, and that question is definitely more documented so I'll leave that out for now. When you create your unit file, just symlink it into the target.wants directory or add it to the Wants= directive.

  • I followed the above to create a new target and added a few targets to my service. However, none of those services is started up by systemd. Is there a complete example that demonstrates services within the newly added target working? – linuxfan Jan 5 '17 at 18:14
  • 1
    See necromancer's stack post if you're looking for simple steps to set up a custom service to run at boot. – Pilbox Jan 28 '17 at 5:17
  • @linuxfan I add foo.target to the Requires field and Before field of multi-user.target, and it like Requires: base.target foo.target. After that I reboot my Arch Linux vm, and foobar.service will run as daemon automatically. – L_K Jun 22 '17 at 1:30
  • 1
    Nice answer, but it doesn't actually seem to work as you'd expect. 1. Just because it's in wants doesn't mean the service will start with that target. 2. The multi-user.target seems to have some kind of hidden things that make it work. – Otheus Aug 17 '17 at 11:56

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.