在linux开机自启动中通常可以添加脚本在rc.local中,所以需要自己生产并注册
    sudo vim /etc/rc.local
    将一下内容复制进文件
    注意:注释不能删除

    1. #!/bin/sh -e
    2. #
    3. # rc.local
    4. #
    5. # This script is executed at the end of each multiuser runlevel.
    6. # Make sure that the script will "exit 0" on success or any other
    7. # value on error.
    8. #
    9. # In order to enable or disable this script just change the execution
    10. # bits.
    11. #
    12. # By default this script does nothing.
    13. echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
    14. exit 0

    该服务启动时使用root账户运行命令。
    运行失败很有可能是命令找不到: sudo systemctl status rc.local.service查看失败原因
    运行可执行程序使用绝对路径(即使path中添加了路径,运行可能找不到)

    1. sudo chmod +x /etc/rc.local

    重启并检查test.log文件
    cat /usr/local/test.log