When should the option RemainAfterExit needs to be set true when creating new systemd services?

南楼画角 提交于 2019-12-04 00:26:33

问题


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).


回答1:


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. On start, it create the flag file, then exists, but the service is kept as 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 will again to its work (cleanup).




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/38072849/when-should-the-option-remainafterexit-needs-to-be-set-true-when-creating-new-sy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!