Ubuntu 配置开机自启动服务全流程
在 Ubuntu 操作系统中,创建并配置服务以实现开机自启动的具体方法如下:
首先,需要在 [/lib/systemd/system] 目录下新建一个名为 sandtable.service 的服务单元文件。
[Unit]
Description=/etc/sandtable Compatibility
Documentation=man:systemd-sandtable-generator(8)
ConditionFileIsExecutable=/etc/sandtable
After=network.target
[Service]
Type=forking
ExecStart=/etc/sandtable start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
[Install]
WantedBy=multi-user.target
Alias=sandtable.service
其次,需要创建用于启动该服务的执行脚本。
在 [/etc] 文件夹下建立 sandtable 启动脚本时,务必在命令末尾加上 [&] 符号,以确保服务在后台运行且不阻塞系统启动流程;否则可能导致系统无法正常完成启动过程。
#!/bin/sh
/home/ubuntu/setup >> /home/ubuntu/test.log&
接下来是启动已配置的服务。
systemctl start sandtable.service #启动服务
systemctl status sandtable.service #查看服务是否启动
systemctl enable sandtable.service #开机运行服务
systemctl is-enabled sandtable.service #查询服务是否开机启动
4 服务常用命令
systemctl is-enabled servicename.service #查询服务是否开机启动
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
对于桌面系统,可以通过特定命令开启或关闭图形界面模式。
// 关闭桌面系统
sudo systemctl set-default multi-user.target
sudo reboot
// 打开桌面系统
sudo systemctl set-default graphical.target
sudo reboot
如果是桌面版系统,相关的 WiFi 配置信息可以在指定路径下进行修改和替换。
/etc/NetworkManager/system-connections
至此,关于 Ubuntu 开机自启动服务设置的介绍已完成,更多相关内容可参考本站历史文章或继续浏览后续内容,感谢大家的支持!