方法一:配置rc.local(不推荐)
直接修订文件/etc/rc.local
,添加操作命令即可
方法二:chkconfig
新建脚本,开头加入以下代码
#!/bin/sh
#chkconfig:2345 80 90
#description:auto_run
将文件移到/etc/init.d
目录下,chmod +x script.sh设置脚本权限
使用chkconfig命令将脚本添加到开机启动
chkconfig --add script.sh
chkconfig script.sh on
方法三:systemctl
-
新建脚本,例如,
/tmp/mytest.sh
,设置权限chmod +x /tmp/mytest.sh
-
进入目录
/usr/lib/systemd/system
,新建服务,例如mytest.service
,编辑为如下形式:[Unit] Description=mytest for auto start Wants=network-online.target [Service] User=root Type=forking ExecStart=/usr/bin/bash /tmp/mytest.sh start ExecStop=/usr/bin/bash /tmp/mytest.sh stop [Install] WantedBy=multi-user.target
-
重新加载systemd配置
systemctl daemon-reload
-
添加开机自启动
systemctl enable mytest.service
-
reboot重启验证
方法四:profile
直接将写好的脚本(.sh文件)放到目录/etc/profile.d/
下。
参考资料:
https://blog.csdn.net/qq_36823799/article/details/117930664