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

问题:设定了一个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 中文手册

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值