在AMD PetaLinux的systemd里启动后台命令的注意事项

由于客户需要,需要在Linux里启动服务,使一个脚本在后台一直运行。

于是创建一个systemd服务,使其运行顶层脚本,再在其中以后台运行方式启动另外一个长期运行脚本。

结果发现,后台运行的长期运行脚本,过一段时间也退出了。

经过调试和分析,发现systemd创建的后台进程,以“Type=forking”方式运行,就不会退出。

按文档 systemd.service , “Type=forking” 表示子进程作为服务的主进程。这说明以“Type=forking”方式运行的后台进程,是正确的模式。

 •   If set to forking, it is expected that the process
               configured with ExecStart= will call fork() as part of
               its start-up. The parent process is expected to exit when
               start-up is complete and all communication channels are
               set up. The child continues to run as the main service
               process, and the service manager will consider the unit
               started when the parent process exits. This is the
               behavior of traditional UNIX services. If this setting is
               used, it is recommended to also use the PIDFile= option,
               so that systemd can reliably identify the main process of
               the service. systemd will proceed with starting follow-up
               units as soon as the parent process exits.
posted @ 2023-08-07 16:02  HankFu  阅读(91)  评论(0编辑  收藏  举报