CompTIA Linux+ (XK0-006)Services and User ManagementMedium

A system administrator needs to configure a `systemd` service named `myservice.service` to automatically restart whenever its main process exits unexpectedly (e.g., due to a crash). Which directive should be added to the `[Service]` section of the unit file?

  1. AExecReload=/bin/kill -HUP $MAINPID
  2. BRestart=on-failure
  3. CRemainAfterExit=yes
  4. DStartLimitIntervalSec=0
Show answer & explanation

Correct answer: B. Restart=on-failure

The `Restart=` directive in the `[Service]` section controls when and how `systemd` should restart a service. Setting it to `on-failure` ensures that the service is restarted only if it exits with a non-zero status code, is terminated by a signal (except `SIGHUP`, `SIGINT`, `SIGTERM`), or if a timeout occurs.

Why the other options are wrong

  • A. `ExecReload=` specifies a command to run when the service is reloaded, not when it crashes and needs a full restart.
  • C. `RemainAfterExit=yes` makes `systemd` consider a service active even after its main process exits, which is useful for `oneshot` services, but does not cause restarts.
  • D. `StartLimitIntervalSec=` and `StartLimitBurst=` control how often `systemd` attempts to restart a service within a given timeframe to prevent a 'flapping' service from consuming resources, but do not *initiate* the restart policy.

systemd Restart Policies

The `Restart=` directive in a `systemd` service unit's `[Service]` section defines when `systemd` should automatically restart the service's main process.

  • Common values: `no`, `on-success`, `on-failure`, `always`.
  • `on-failure` restarts on non-zero exit code or signal.
  • Prevents service downtime from crashes.

Memory trick: RESTART a service ON-FAILURE to keep it alive.

More Services and User Management questions