How to Configure systemd to Restart a Service on Failure
A server runs a custom application that listens on TCP port 8080. The administrator wants to ensure the application starts automatically on boot and restarts if it crashes. Which systemd unit file directive should be used to achieve the restart behavior?
Quick Answer
The answer is `Restart=on-failure`, which is the correct systemd directive to configure a service to restart automatically when it crashes. This directive works by instructing systemd to restart the unit whenever the process exits with a non-zero exit code, is terminated by a signal (such as SIGKILL), or hits a timeout—exactly the conditions that define a crash for a TCP application listening on port 8080. On the Linux Foundation Certified System Administrator LFCS exam, this question tests your understanding of service reliability and unit file options, often appearing alongside `Restart=always` as a distractor; the key trap is that `always` restarts even on clean exits, which is unnecessary here. Remember that `on-failure` is the precise choice for crash recovery because it ignores intentional stops. A helpful mnemonic: “Failure triggers the fix—on-failure is your restart pick.”
⚠ Common exam trap
Many exam-takers confuse `RestartSec` with the restart policy itself, or assume `Type=notify` or `RemainAfterExit=yes` imply automatic restart behavior, when in fact only `Restart=` directives control restart logic.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Restart=on-failure
The `Restart=on-failure` directive in a systemd unit file instructs systemd to automatically restart the service unit when it exits with a non-zero exit code, is terminated by a signal (including SIGKILL), or times out. This directly satisfies the requirement for the application to restart if it crashes, as a crash typically results in an unclean exit that triggers the restart condition.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
RestartSec=5
Why it's wrong here
RestartSec sets the delay before restart, but does not enable restarting.
- ✗
Type=notify
Why it's wrong here
Type=notify is used for services that notify systemd when they are ready, unrelated to restart.
- ✗
RemainAfterExit=yes
Why it's wrong here
RemainAfterExit keeps the service as active even if the main process exits, but does not restart it.
- ✓
Restart=on-failure
Why this is correct
This directive tells systemd to restart the service when it exits unexpectedly.
Go deeper
Related to this question
About these practice questions
One of 507 original LFCS practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on LFCS
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A system administrator is configuring a custom systemd service that runs a Python script. The script logs output to stdout. The administrator wants to ensure that the service restarts automatically if it crashes, but only after a 10-second delay. Which directive should be added to the [Service] section of the unit file?
hard- A.Restart=on-success RestartSec=10
- ✓ B.Restart=on-failure RestartSec=10
- C.Restart=always RestartSec=10
- D.RestartSec=10
Why B: `Restart=on-failure` ensures the service restarts only when it exits with a non-zero exit code or is terminated by a signal (e.g., SIGKILL), which matches the 'crashes' scenario. Adding `RestartSec=10` introduces a 10-second delay before the restart attempt, as required. The other options either restart on success (irrelevant) or always restart (which would restart even on intentional stops), or omit the restart condition entirely.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This LFCS practice question is part of Courseiva's free Linux Foundation certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the LFCS exam.