Systemd: enable, start (wait) status

systemdtimeout

If I do this in a script, the call to status shows "OK"

  1. systemctl enable foo
  2. systemctl start foo
  3. systemctl status foo

But only a few milliseconds later the status is "failed".

How can I detect if the start was successful?

What means "successful" for me here: The server started to run.

Systemd seams to start the process and does not wait to see if the server was able to start correctly.

Example: The config of the server contains a syntax error. The server will run for some milliseconds but then terminate.

The most simple solution would be to execute "sleep 1" before my call to "systemctl status foo".

But this feels dirty.

Maybe there is a better solution to get the real status after starting the server.

I only care about the starting. If the server fails two hours later, this is not part of this question.

Best Answer

Usually, you get failed state in that command output for started process, when:

  1. Process exits with status which is not equal to 0 or with status, different from SuccessExitStatus parameter specified in unit file's [Service] section
  2. Unclean signal sent to the daemon (kill etc)
  3. Timeout when starting daemon, can be set via TimeoutStartSec or TimeoutSec, default is value of DefaultTimeoutStartSec, which usually is 90 seconds or as set in system.conf, [Manager] section
  4. Watchdog (when type=notify), same as above, controlled by WatchdogSec, RuntimeWatchdogSec

The service must call sd_notify(3) regularly with "WATCHDOG=1" (i.e. the "keep-alive ping"). If the time between two such calls is larger than the configured time, then the service is placed in a failed state

  1. typeis incorrectly set in unit file

  2. GuessMainPID

Takes a boolean value that specifies whether systemd should try to guess the main PID of a service if it cannot be determined reliably. This option is ignored unless Type=forking is set and PIDFile= is unset because for the other types or with an explicitly configured PID file, the main PID is always known. The guessing algorithm might come to incorrect conclusions if a daemon consists of more than one process. If the main PID cannot be determined, failure detection and automatic restarting of a service will not work reliably. Defaults to yes.