先了解一下Linux一些脚本启动的顺序
/etc/rc.local -> /etc/profile -> 自定义脚本
通过系统提供的自启动脚本
(这种方式适合添加需要开机自启的应用程序,比如 redis、nginx 等等)
Linux 系统提供了一个开机启动的脚本( /etc/rc.local 或 /etc/rc.d/rc.local 这两个文件实际上是一个文件)
1、编辑 /etc/rc.local
vi /etc/rc.local
2、添加要执行的命令
注意:这里的执行命令都必须是全路径的,就算你添加到了 PATH 路径,自启动的时候,也是识别不到的(因为启动顺序是 /etc/rc.local -> /etc/profile )
例如,我这里添加的 redis 自启动:
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using thisfile.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
#Please note that you must run'chmod +x /etc/rc.d/rc.local'to ensur