40

I am trying to write a few services. Some of them have 'type' option set to oneshot. But i am still confused when the option 'RemainAfterExit' needs to be set true. (not just that service needs to be active even after exiting).

Aditya Vardhan
  • 531
  • 1
  • 4
  • 7
  • 2
    If your service is wanted or required by other services, `RemainAfterExit=yes` seems make sense to avoid the service to be started once again. Also in case if a `ExecStop=` is set to do reverse tasks. – Shuangistan Jun 30 '16 at 06:59
  • 1
    These service files define ways to achieve specific computing results. By definition, creating and editing them is programming. – R.D. Alkire Apr 03 '21 at 21:21

3 Answers3

55

Use RemainAfterExit=yes for services, which somehow change state of the system. When you want that state reverted, you just stop the service. Then you can start it again, but not without first stopping it. An example would be service which creates a flag in filesystem to be used by some other application. When started, it creates the flag file, then exits, but the service is considered active by systemd. Then you can stop it and it will remove the flag file.

Use RemainAfterExit=no for services, which do some action, but do not change the state of the system. An example would be a service to cleanup /tmp. You start it, it will do its work and then be inactive (no need to stop it). And you can start it again anytime and it will again do its work (cleanup).

Marki555
  • 6,434
  • 3
  • 37
  • 59
13

From Systemd page below is the description of RemainAfterExit

RemainAfterExit= Takes a boolean value that specifies whether the service shall be considered active even when all its processes exited. Defaults to no.

This simply means that if all your processes started from service exits, when you query the status of the service, its still says active if this flag is set to true. If set to false, then service status is inactive.

This is particularly useful when you are adding dependency on services during bootup. wherein the service status is important to start dependent services.

DarkKnight
  • 597
  • 3
  • 15
0

The flag is useful for your own bookkeeping and accounting. If it's useful to you to consider the service active after's finished, you can set the flag.

Mark Stosberg
  • 12,961
  • 6
  • 44
  • 49