When working with systemd service units, it's common to need dependencies on multiple services. The syntax you've shown is actually correct - systemd fully supports multiple After=
directives in the [Unit] section. Here's why it works:
[Unit]
Description=My Java Application
After=network.target syslog.target
Wants=network.target
There are actually two valid ways to specify multiple dependencies:
# Option 1: Multiple After lines
After=network.target
After=syslog.target
# Option 2: Space-separated in single line
After=network.target syslog.target
When creating dependencies, consider these best practices:
- Always include
Wants=
along withAfter=
for proper dependency resolution - The order matters - systemd will process dependencies in the order they appear
- For network services, consider using
network-online.target
instead of justnetwork.target
Regarding your question about Restart=
, here's a typical configuration for Java applications:
[Service]
Restart=on-failure
RestartSec=10
This tells systemd to:
- Restart on non-clean exits (exit code != 0)
- Wait 10 seconds between restart attempts
- Prevent rapid restart loops
Here's a complete, production-ready example:
[Unit]
Description=Spring Boot Application
After=network-online.target syslog.target postgresql.service
Wants=network-online.target postgresql.service
[Service]
User=appuser
Group=appgroup
ExecStart=/usr/bin/java -jar /opt/myapp/app.jar
SuccessExitStatus=143
Restart=on-failure
RestartSec=10
Environment=SPRING_PROFILES_ACTIVE=prod
[Install]
WantedBy=multi-user.target
Key features in this example:
- Depends on network, logging and PostgreSQL
- Proper user/group security context
- Environment variable configuration
- Automatic restart policy
- Clean installation target
When configuring systemd services, it's common to require multiple dependencies before your service starts. The example shows perfectly valid syntax where we declare dependencies on both networking and logging systems:
[Unit]
Description=Spring Boot Application
Wants=network.target
After=network.target
After=syslog.target
This configuration ensures our Java application won't start until both network connectivity and system logging are available.
There are actually three equivalent ways to specify multiple After dependencies:
# Method 1: Separate After directives
After=network.target
After=syslog.target
# Method 2: Space-separated in single line
After=network.target syslog.target
# Method 3: Newline-separated values
After=network.target
syslog.target
The example shows Wants=network.target
rather than Requires
. This is important because:
- Wants: Attempts to start dependency but continues if it fails
- Requires: Fails if dependency cannot be started
The original question asks about Restart
directives. Here's a complete example with restart policy:
[Unit]
Description=My Critical Service
After=network.target postgresql.service redis.service
[Service]
ExecStart=/usr/bin/my-service
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
Here's how you might define dependencies for a web application that needs database and cache services:
[Unit]
Description=Node.js Web Server
After=network.target
After=mongodb.service
After=redis.service
Requires=mongodb.service
[Service]
Environment=NODE_ENV=production
WorkingDirectory=/opt/app
ExecStart=/usr/bin/node server.js
Restart=always
User=nodeuser
[Install]
WantedBy=multi-user.target
After creating your service, verify the dependency chain with:
systemctl list-dependencies your-service.service