在 Ubuntu 系统中,自启动脚本可以通过多种方式进行管理,具体查看方法取决于脚本的存放位置和使用的初始化系统(如 systemd
、cron
、rc.local
等)。以下是几种常见的方法:
✅ **1. 查看 systemd
自启动服务(Ubuntu 16.04 及以上版本)
① 列出所有已启用的自启动服务:
systemctl list-unit-files --state=enabled
② 查看某个特定服务的状态:
systemctl status 服务名
例如查看 nginx
服务的状态:
systemctl status nginx
③ 列出用户级别的自启动服务:
systemctl --user list-units --type=service
④ 检查服务文件路径:
- 系统级服务文件通常位于:
/etc/systemd/system/ /lib/systemd/system/
- 用户级服务文件通常位于:
~/.config/systemd/user/
✅ **2. 查看 /etc/rc.local
自启动脚本
Ubuntu 18.04 及以下版本默认支持 rc.local
。
- 查看
rc.local
文件内容:
cat /etc/rc.local
- 如果
rc.local
不存在,可以手动创建:
sudo nano /etc/rc.local
确保文件头部有以下内容:
#!/bin/bash
# rc.local
# 添加自定义启动命令
echo "System is booting" >> /var/log/boot.log
exit 0
- 赋予执行权限:
sudo chmod +x /etc/rc.local
- 手动启用
rc.local
(如需):
sudo systemctl enable rc-local
sudo systemctl start rc-local
✅ **3. 查看 cron
定时任务自启动脚本
① 查看系统级 cron
自启动任务:
sudo cat /etc/crontab
② 查看用户级 cron
自启动任务:
crontab -l
③ 查看 /etc/cron.d
中的自定义任务:
ls /etc/cron.d/
cat /etc/cron.d/文件名
例如,检查是否有类似以下自启动项:
@reboot /path/to/script.sh
✅ **4. 查看 /etc/profile
和 /etc/profile.d/
登录自启动脚本
这些文件会在用户登录时执行,适用于全局环境变量或脚本。
① 查看全局登录脚本:
cat /etc/profile
② 查看用户级别的 .bashrc
和 .profile
:
cat ~/.bashrc
cat ~/.profile
✅ **5. 查看 /etc/init.d/
目录下的旧式自启动脚本
Ubuntu 15.04 以前使用 init
系统,仍可能存在旧式脚本。
- 列出已有服务:
ls /etc/init.d/
- 查看某个具体服务的启动逻辑:
cat /etc/init.d/脚本名
📌 总结
- 主要检查点:
systemd
服务 (/etc/systemd/system/
)rc.local
文件 (/etc/rc.local
)cron
定时任务 (crontab -l
)- 登录脚本 (
/etc/profile
,~/.bashrc
) - 旧式脚本 (
/etc/init.d/
)