systemd service with multiple After

The unit file you posted looks fine. But the unit as you defined it here has no strict dependencies, only weak (Wants= instead of Requires=). That means if is not there or if it fails to start, this unit would be started anyway. After= (and Before=) is used only for ordering, not for dependency management. So if your app needs another service, use Requires=. If it needs that service before it can be started itself, use After= 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 is systemctl daemon-reload. To enable the unit type systemctl enable my-unit.service. This adds a symlink in /etc/systemd/system/ 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, like on-failure or always (more in the man page of systemd.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.

Leave a Comment