diff --git a/man/systemd-run.xml b/man/systemd-run.xml index 610c3acd37..3a1d18dae9 100644 --- a/man/systemd-run.xml +++ b/man/systemd-run.xml @@ -399,8 +399,13 @@ Exit status - On success, 0 is returned, a non-zero failure - code otherwise. + On success, 0 is returned. If systemd-run failed to start the service, a + non-zero return value will be returned. If systemd-run waits for the service to + terminate, the return value will be propagated from the service. 0 will be returned on success, including + all the cases where systemd considers a service to have exited cleanly, see the discussion of + SuccessExitStatus= in + systemd.service5. + @@ -503,6 +508,16 @@ There is a screen on: $ loginctl enable-linger + + + Return value + + $ systemd-run --user --wait true +$ systemd-run --user --wait -p SuccessExitStatus=11 bash -c 'exit 11' +$ systemd-run --user --wait -p SuccessExitStatus=SIGUSR1 bash -c 'kill -SIGUSR1 $$$$' + + Those three invocations will succeed, i.e. terminate with an exit code of 0. + diff --git a/src/run/run.c b/src/run/run.c index 3d63cf0254..de968caf3f 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -16,6 +16,7 @@ #include "bus-wait-for-jobs.h" #include "calendarspec.h" #include "env-util.h" +#include "exit-status.h" #include "fd-util.h" #include "format-util.h" #include "main-func.h" @@ -1303,9 +1304,14 @@ static int start_transient_service( } } - /* Try to propagate the service's return value */ - if (c.result && STR_IN_SET(c.result, "success", "exit-code") && c.exit_code == CLD_EXITED) + /* Try to propagate the service's return value. But if the service defines + * e.g. SuccessExitStatus, honour this, and return 0 to mean "success". */ + if (streq_ptr(c.result, "success")) + *retval = 0; + else if (streq_ptr(c.result, "exit-code") && c.exit_status > 0) *retval = c.exit_status; + else if (streq_ptr(c.result, "signal")) + *retval = EXIT_EXCEPTION; else *retval = EXIT_FAILURE; }