无法禁用systemd串行-getty服务

22
在安装了Arch Linux的树莓派上,有一个名为serial-getty@AMA0的服务正在运行。

该单元文件位于:/usr/lib/systemd/system/serial-getty@.service

作为root用户,我可以执行以下命令:


```bash systemctl start serial-getty@AMA0.service ```
systemctl stop serial-getty@ttyAMA0
systemctl disable serial-getty@ttyAMA0

重启后,该服务被重新启用并运行。

为什么在禁用服务后它又被启用了呢?如何永久禁用它?

更新

systemd 在 /usr/lib/systemd/system-generators/ 使用生成器。其中一个二进制文件叫做 systemd-getty-generator。该二进制文件在系统启动时运行,并将符号链接 serial-getty@ttyAMA0.service 添加到 /run/systemd/generator/getty.target.wants 中。

我最终找到了一个简单粗暴的解决方案。我将 /usr/lib/systemd/system/serial-getty@.service 中的所有操作都注释掉了。该服务似乎仍然启动了,但没有阻塞 ttyAMA0。


1
看一下符号链接指向的/usr/lib/systemd/system/serial-getty@.service的位置。 - Sergey Kanaev
2个回答

43

防止某个服务再次启用的正确方法是使用:

systemctl mask serial-getty@ttyAMA0.service

(在这种情况下以ttyAMA0为例)。这将在该服务的条目中添加一个指向null的链接。


在编写systemd服务时,是否可以省略“.service”后缀? - Peter Mortensen

-5

尝试这段代码:

system("systemctl stop serial-getty@ttyAMA0.service");
system("systemctl disable serial-getty@ttyAMA0.service");

我使用它,效果很好。


2
上下文是什么?来自Bash脚本?Python脚本?Perl脚本?C程序?还是其他什么? - Peter Mortensen
是的,您可以在Linux中使用C++的system()。例如,您可以使用system(" data")、system(" dir")、system(" ls")等命令来运行Bash脚本(Linux中的系统脚本)。 - m-tech
我已经很久没有登录了,只是为了留下这个评论。请不要使用C语言的system函数,因为它有着众所周知的安全问题。在这种情况下尤其糟糕,因为操作systemd通常需要root权限。编辑:为了更好的理解,请参考以下链接https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152177 - Woodrow Douglass

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接