systemd service with multiple After

30,080

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 network.target 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/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, 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.

Share:
30,080

Related videos on Youtube

naXa stands with Ukraine
Author by

naXa stands with Ukraine

Updated on September 18, 2022

Comments

  • naXa stands with Ukraine
    naXa stands with Ukraine over 1 year

    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?