一、编写测试脚本
sudo vim /opt/sh/autoRun.sh
#!/bin/bash
date >> /opt/sh/autoRun.log
exit 0
二、设置权限
sudo chmod 755 /opt/sh/autoRun.sh
三、配置自启动
自定义启动文件名称(rc-local.service)
注意:如果调用其他命令,如java -jar,由于部分环境变量未生效,所以需要写全路径
如: /opt/java/java8/bin/java -jar xxxxx
sudo systemctl enable rc-local.service
#创建 rc.local
sudo touch /etc/rc.local
#设置权限
sudo chmod 755 /etc/rc.local
sudo cat "#!/bin/bash" > /etc/rc.local
四、设置rc-local.service开机自启
sudo vim /etc/systemd/system/rc-local.service
[Unit]
After=network.target
[Service]
ExecStart=/opt/sh/autoRun.sh
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
sudo systemctl enable rc-local.service
sudo systemctl status rc-local.service
注意:后缀名一定要是service ,否则会报错