Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between systemd's 'oneshot' and 'simple' service types?

Tags:

systemd

What is the difference between systemd service Type oneshot and simple? This link states to use simple instead of oneshot for timers. I am not able to understand it correctly.

like image 203
Dinesh P.R. Avatar asked Aug 19 '16 06:08

Dinesh P.R.


People also ask

What is a oneshot service?

The Type=oneshot service unit: blocks on a start operation until the first process exits, and its state will be reported as "activating"; once the first process exits, transitions from "activating" straight to "inactive", unless RemainAfterExit=true is set (in which case it becomes "active" with no processes!);

What is Type simple in systemd?

This is important because it tells systemd how to correctly manage the servie and find out its state. The Type= directive can be one of the following: simple: The main process of the service is specified in the start line. This is the default if the Type= and Busname= directives are not set, but the ExecStart= is set.

What is ExecStart Service file?

ExecStart. The command to run to start the service. This includes the full path to the command and arguments to modify the service. The resulting [Service] section looks like this: [Service] Type=simple ExecStart=/usr/bin/sleep infinity.

What is ExecStop?

The ExecStop setting is optional and is used to communicate with the service for a clean termination. The process specified by ExecStop will run in case the service crashes.


2 Answers

The Type=oneshot service unit:

  • blocks on a start operation until the first process exits, and its state will be reported as "activating";

  • once the first process exits, transitions from "activating" straight to "inactive", unless RemainAfterExit=true is set (in which case it becomes "active" with no processes!);

  • may have any number (0 or more) of ExecStart= directives which will be executed sequentially (waiting for each started process to exit before starting the next one);

  • may leave out ExecStart= but have ExecStop= (useful together with RemainAfterExit=true for arranging things to run on system shutdown).

The Type=simple service unit:

  • does not block on a start operation (i. e. becomes "active" immediately after forking off the first process, even if it is still initializing!);

  • once the first process exits, transitions from "active" to "inactive" (there is no RemainAfterExit= option);

  • is generally discouraged because there is no way to distinguish situations like "exited on start because of a configuration error" from "crashed after 500ms of runtime" and suchlike.

Both Type=oneshot and Type=simple units:

  • ignore any children of the first process, so do not use these modes with forking processes (note: you may use Type=oneshot with KillMode=none, but only do this if you know what you are doing).
like image 101
intelfx Avatar answered Sep 22 '22 11:09

intelfx


From systemd's point of view, Type=simple is kind of fire and forget. Systemd just forks a process defined in ExecStart= and goes on its way, even if the process fails to start.

like image 37
Umut Avatar answered Sep 23 '22 11:09

Umut