方法一:配置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