-
切换至root,建立rc-local.service文件
sudo -i vim /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
-
然后创建文件:rc.local
vim /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. #start script sudo -H -u user1 /bin/bash -c "vncserver :11 -localhost no -geometry 1440x900" sudo -H -u user2 /bin/bash -c "vncserver :12 -localhost no -geometry 1440x900"#end script echo "added sucessfully!" > /tmp/added_script.log exit 0
-
加上权限,使服务自启动并开启服务
chmod +x /etc/rc.local systemctl enable rc-local systemctl start rc-local.service
-
查看状态,注意active (running)
systemctl status rc-local.service
-
重启并检查test.log文件,如果里面有内容(added sucessfully!),则自启动脚本安装成功。
reboot vim /tmp/added_script.log
也可以在“/etc/profile”下写脚本