在linux开机自启动中通常可以添加脚本在rc.local中,所以需要自己生产并注册sudo 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.echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.logexit 0
该服务启动时使用root账户运行命令。
运行失败很有可能是命令找不到: sudo systemctl status rc.local.service查看失败原因
运行可执行程序使用绝对路径(即使path中添加了路径,运行可能找不到)
sudo chmod +x /etc/rc.local
重启并检查test.log文件cat /usr/local/test.log
