UBUNTU开机自动运行程序
在较新的 Ubuntu 版本(如 16.04 及以后)中,rc.local 文件默认不存在,因为这些系统使用 systemd 作为初始化系统,取代了传统的 SysVinit 机制。如果需要使用 rc.local 方法,可以按照以下步骤操作:
手动创建并配置 rc.local
创建 rc.local 文件
sudo nano /etc/rc.local
添加以下内容
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/path/to/your/script.sh & # 添加你的脚本路径
exit 0
保存并退出编辑器,然后设置文件权限:
sudo chmod +x /etc/rc.local
创建并配置 systemd 服务
sudo nano /etc/systemd/system/rc-local.service
添加以下内容到服务文件
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
启用并启动服务
sudo systemctl enable rc-local
sudo systemctl start rc-local.service
验证服务状态
sudo systemctl status rc-local.service