A system administrator needs to ensure that a specific service, 'myapp', starts automatically after a system crash and also restarts if it fails. Which systemd unit directive should be used to achieve this behavior?
Trap 1: RemainAfterExit=yes
RemainAfterExit=yes only keeps the service unit active after the main process exits, but does not restart it.
Trap 2: Restart=always
Restart=always restarts the service regardless of exit status, but it also restarts if the admin stops it manually, which is not desired.
Trap 3: ExecStopPost=/bin/systemctl restart myapp.service
ExecStopPost runs a command after the service stops, but it is not the standard way to handle automatic restart on failure.
- A
RemainAfterExit=yes
Why wrong: RemainAfterExit=yes only keeps the service unit active after the main process exits, but does not restart it.
- B
Restart=always
Why wrong: Restart=always restarts the service regardless of exit status, but it also restarts if the admin stops it manually, which is not desired.
- C
Restart=on-failure and WantedBy=multi-user.target
Restart=on-failure restarts the service only if it fails (non-zero exit), and WantedBy=multi-user.target ensures it starts at boot.
- D
ExecStopPost=/bin/systemctl restart myapp.service
Why wrong: ExecStopPost runs a command after the service stops, but it is not the standard way to handle automatic restart on failure.