- 创建一个文件 setup.sh
- 将下面的内容复制进 setup.sh
#!/bin/bash
echo 创建服务文件
sudo touch /usr/lib/systemd/system/rc-local.service
sudo echo '''[Unit]
Description="/etc/rc.local Compatibility"
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardInput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target''' > /usr/lib/systemd/system/rc-local.service
echo 启用服务
sudo systemctl enable rc-local.service
echo 创建rc.local文件
sudo touch /etc/rc.local
echo '''#!/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.
# -----------替换横杠间的内容为需要开机执行的命令---------------
echo rc.local 文件正常!
# -----------替换横杠间的内容为需要开机执行的命令---------------
exit 0''' > /etc/rc.local
echo 授权
sudo chmod +x /etc/rc.local
echo 测试
sudo sh /etc/rc.local
- 在setup.sh的路径下打开终端
- 在终端中执行 chmod +x setup.sh
- 然后 再运行脚本 ./setup.sh