[systemd-devel] When does a systemctl start ... returns to prompt?

Michael Chapman mike at very.puzzling.org
Wed Aug 29 08:02:57 UTC 2018


On Wed, 29 Aug 2018, Wojtek Swiatek wrote:
> Hello everyone
> 
> systemctl start myserv.service sometimes immediately returns to the shell
> prompt and sometimes stays until the program is done. Specifically, taking
> the example of two programs
> 
> - prog_one which starts in the foreground and never ends, defined as
> ExecStart=/path/to/prog_one
> 
> - prog_two and prog_three which are defined as
> Type=oneshot
> ExecStart=/path/to/prog_two
> ExecStartPost=/path/to/prog_three
> 
> systemctl start prog_one.service immediately returns (and prog_one keeps
> running)
> systemctl start prog_two.service does not return, waits until prog_two and
> then progr_three finish before returning
> 
> If I do not use a Type in the prog_one unit, the unit fails because
> prog_three is started right after prog_one is initialized (and still
> running)
> 
> Question 1: what are the rules for systemctl start ... to immediately come
> back to the prompt or not

systemctl waits until the service has finished activating. As you've 
noted, this is governed by the Type property. For a Type=oneshot service, 
the service finishes activating once all of its ExecStartPre=, ExecStart= 
and ExecStartPost= commands have exited successfully.

Essentially, it's exactly the same rule used by the ordering properties 
After= and Before=.

> Question 2: how can I configure the prog_two/prog_three case, i.e. having
> them starting one after the other (= start prog_three when prog_two is
> done), and have the prompt return immediately

You can use "systemctl --no-block" to have systemctl not wait for the 
service to finish activating. The downside to this is that you will have 
no indication of a problem if the service fails during activation.

Another option is to change a Type=oneshot service to Type=simple. In this 
case systemd does not wait for any startup notification from the 
ExecStart= command; it proceeds to execute any ExecStartPost= commands 
immediately.

However, take note that Type=simple services can have only a single 
ExecStart= command.


More information about the systemd-devel mailing list