Something not mentioned in previous answers is that in order to get a custom systemd target to work like e.g. multi-user.target on boot, you have to specify it as part of the boot process in some way.
Basically what you want to do is check what your current default is: systemctl get-default
. It will most probably be graphical.target
or multi-user.target
.
You then need to create a symlink in /etc/systemd/system
to your new default target and make sure that your target is an extension of the boot process.
ln -s /etc/systemd/system/foo.target /etc/systemd/system/default.target
Check with:
systemctl daemon-reload
systemctl get-default
If multi-user.target
was your previous default you'll want to have it configured in your custom unit like this, as mentioned by m32:
[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
Note the important bit here is that multi-user.target is specified as Requires. See the answer from @m32 for details on what everything means.
What you are doing here is extending the boot process with your custom unit. Setting it as the default for systemd instructs systemd to load that unit on boot. The configuration of that target then pulls in any other targets which goes on until it reaches the most basic targets. At least that's how I understand it now.
Then like mentioned in the other answers you can go about specifying your units in the Wants=
directive in foo.target
and add the WantedBy=foo.target
to the install section of your units. Note that you still need to activate any units you want to start on boot with systemd enable foobar.service
.
So to sum it up:
- Create your target as extension of existing targets
- Create the symlink to make it your default target
- Specify that target in all units you want to start with it
- Enable any units you want to start at boot that way
This document about runlevels has a bit more details: https://www.landoflinux.com/linux_runlevels_systemd.html
foo.target
to theRequires
field andBefore
field ofmulti-user.target
, and it likeRequires: base.target foo.target
. After that I reboot my Arch Linux vm, andfoobar.service
will run as daemon automatically. – L_K Jun 22 '17 at 01:30