Is it possible to create a service that depends on several other services? What is the right syntax?
Such as:
[Unit]
Description=service description
Wants=network.target
After=network.target
After=syslog.target
[Service]
User=bootapp
ExecStart=/var/app/app.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
The goal is to start a service automatically after OS reboot and make sure that logging and networking services are started prior to the service.
Additional question: Do I need to specify Restart
?
The unit file you posted looks fine. But the unit as you defined it here has no strict dependencies, only weak (
Wants=
instead ofRequires=
). That means if network.target is not there or if it fails to start, this unit would be started anyway.After=
(andBefore=
) is used only for ordering, not for dependency management. So if your app needs another service, useRequires=
. If it needs that service before it can be started itself, useAfter=
additionally.To enable your unit to start automatically after booting, you have to enable it. Systemd needs to know where to link it for starting, that's what
WantedBy=
in the[Install]
section is used for. After editing the unit file and saving it in/etc/systemd/system/my-unit.service
you have to reload the systemd daemon to have it pick up the new unit, before you can enable it; the command issystemctl daemon-reload
. To enable the unit typesystemctl enable my-unit.service
. This adds a symlink in/etc/systemd/system/multi-user.target.wants/
to your unit file.To start it manually you can type
systemctl start my-unit.service
.Restart=
is only needed if you want your app to be automatically restarted, when it exited. There are different possibilities for when to restart, likeon-failure
oralways
(more in the man page ofsystemd.service
).Also your app.jar needs to be executable for this to work. If it is and it starts your app, then it's fine. I think a jar must be started by sth. like
java -jar app.jar
, but ofc. I could be wrong here.