当我运行此命令时
sudo systemctl enable /home/ec2-user/my_custom.service
我明白了
Failed to enable unit: Access denied
当我跑步的时候
systemctl enable /home/ec2-user/my_custom.service
我明白了
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ====
Authentication is required to manage system service or unit files.
Authenticating as: Cloud User (ec2-user)
Password:
==== AUTHENTICATION COMPLETE ====
Failed to enable unit: Access denied
现在我没有任何密码来设置新的使用sudo passwd ec2-user
,然后使用该密码,但仍然出现相同的错误
这是 my_custom.service 的内容
[Unit]
Description=go_responder
After=network.target
[Service]
Type=simple
User=ec2-user
ExecStart=/home/ec2-user/custom_service_executable
[Install]
WantedBy=default.target
答案1
我在 Centos 8 Stream 机器上遇到了类似的问题。结果发现它与 selinux 有关。
您可以通过运行sudo setenforce 0
然后sudo systemctl daemon-reload
再次运行来测试这一点。您现在应该能够启动/启用您的服务(如果问题出在 selinux 上)。
例子:
[root@box ~]# ln -s /path/to/custom.service /etc/systemd/system/
[root@box ~]# systemctl daemon-reload
[root@box ~]# systemctl start oxidized-ng.service
Failed to start custom.service: Unit custom.service not found.
# temporarily setting selinux to permissive
[root@box ~]# setenforce 0
[root@box ~]# systemctl daemon-reload
# systemd can now see the service after a daemon-reload
[root@box ~]# systemctl status custom.service
● custom.service - A service to do all the things...
Loaded: loaded (/opt/foo/systemd/custom.service; linked; vendor preset: disabled)
Active: inactive (dead)
答案2
Systemd 单元必须放置在搜索路径systemd 在其中查找单元文件。它们不能放在任何其他目录中。
将文件复制到/etc/systemd/system
,运行sudo systemctl daemon-reload
,然后您将能够启用您的服务。