欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

systemctl service失效,在start后自动调用stop (ExecStop),排查分析及处理过程

程序员文章站 2022-03-10 22:09:14
...

问题:设定了一个systemctl,start后,发现相关配置没有生效

service代码:

[Unit]
Description=echo test

[Service]
Type=simple
ExecStart=/bin/echo start
ExecStop=/bin/echo stop

[Install]
WantedBy=multi-user.target

配置过程:

1. systemctl enable test.service

2. systemctl damon-reload

3. systemctl start test.service

结果:

发现service里面的服务没有产生对应的结果(我是做了一个crontab的配置,这里就不详细阐述了)

 

排查过程:

1. 确认service文件情况:systemd-analyze verify test.service

      没有发现异常

2.查看日志:journalctl -u test.service

Sep 07 13:23:45 n157-014-142 systemd[1]: Started service to set iptables.
Sep 07 13:23:45 n157-014-142 echo[677120]: start
Sep 07 13:23:45 n157-014-142 echo[677123]: stop

发现日志中先进行了start,紧接着调用了stop。

 

处理方法:

在test.service中增加 RemainAfterExit=yes,在进行测试就没问题了。

分析原因:

查阅了很多资料,都没有找到具体明确的解释。systemctl中service在start (ExecStop)后马上调用了ExecStop的内容,我猜测原因是systemctl status test.device,处于inactive,systemctl在service处于inactive状态时会自动调用ExecStop

所以解决办法就是增加

 RemainAfterExit=yes

这样,service在运行后就会处于active状态,就不会调用stop了。

修改后的代码如下

[Unit]
Description=echo test

[Service]
Type=simple
ExecStart=/bin/echo start
ExecStop=/bin/echo stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

参考资料:

systemctl 中文手册