答案:通过systemctl enable配置开机启动,需确保.service文件正确并位于/etc/systemd/system/,执行daemon-reload后启用服务,WantedBy指定启动目标,disable可禁用开机启动。

在Linux中,让程序或服务在开机时自动运行,主要通过
systemctl enable
解决方案:
确认服务文件存在且正确: 首先,确保你的服务有一个
.service
/etc/systemd/system/
/usr/lib/systemd/system/
my_app.service
[Unit]
[Service]
[Install]
[Unit] Description=My Application After=network.target [Service] ExecStart=/path/to/your/app Restart=on-failure User=youruser [Install] WantedBy=multi-user.target
ExecStart
User
WantedBy=multi-user.target
使用systemctl enable
.service
systemctl enable your_app.service
/etc/systemd/system/multi-user.target.wants/
重新加载systemd配置: 执行
systemctl daemon-reload
启动服务并检查状态: 使用
systemctl start your_app.service
systemctl status your_app.service
status
验证开机启动: 重启系统,或者使用
systemctl reboot
systemctl status your_app.service
通常,系统自带的服务放在
/usr/lib/systemd/system/
/etc/systemd/system/
/usr/lib/systemd/system/
/etc/systemd/system/
/etc/systemd/system/
/usr/lib/systemd/system/
/etc/systemd/system/
WantedBy=multi-user.target
multi-user.target
WantedBy=multi-user.target
basic.target
network.target
graphical.target
reboot.target
poweroff.target
选择哪个target取决于你的服务的依赖关系。比如,如果你的服务依赖于网络,那么
network.target
systemctl list-dependencies multi-user.target
multi-user.target
使用
systemctl disable your_app.service
/etc/systemd/system/multi-user.target.wants/
systemctl stop your_app.service
以上就是如何在Linux中开机启动 Linux systemctl enable配置的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号