設置開機自啟動方法總結
查看命令
chkconfig --list #列出所有的系統服務。CentOS6之前
systemctl list-units --all --type=service #查看所有服務。CentOS6之后
systemctl list-units --type=service #查看所有已經啟動的服務。CentOS6之后
設置自啟動服務
systemctl enable servicename
systemctl start servicename
設置自啟動腳本
/etc/rc.d/rc.local
方法
在 /etc/rc.d/rc.local
或者其鏈接文件 /etc/rc.local
文件中添加啟動命令
問題
在CentOS7之后可能無法自啟動
原因和解決方案
CentOS7之后采用systemd作為init,rc.local
也是通過systemd
的service啟動的。
輸入
cat /usr/lib/systemd/system/rc-local.service
查看其啟動配置
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
其中發現啟動rc.local
是通過命令 ExecStart=/etc/rc.d/rc.local start
實現的,因此 /etc/rc.d/rc.local
必須得有執行權限。
若其沒有執行權限,應給予。
chmod +x /etc/rc.d/rc.local
之后則需要啟動rc-local.service
服務
systemctl enable rc-local.service
systemctl start rc-local.service
若未能成功啟動
systemctl enable rc-local.service
The unit files have no [Install] section. They are not meant to be enabled
using systemctl.
Possible reasons for having this kind of units are:
1) A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
1) A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
1) A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).
提示啟動service里面沒有install這節的內容。那就給它通過多用戶的target啟動就可以了。
vim /usr/lib/system/system/rc-local.service
[Install]
WantedBy=multi-user.target
然后再次enable啟動服務:
systemctl enable rc-local.service
ln -s '/lib/systemd/system/rc-local.service' '/etc/systemd/system/multi-user.target.wants/rc-local.service'
以上解決方法摘錄自網絡,systemd的原理我並不了解
擴展
啟動腳本可以寫成服務然后用systemd添加到自啟動,不一定非要添加到rc.local
/etc/init.d/
方法
此方法貌似是sysv的init使用,systemd行不行有待試驗
將腳本拷貝至 /etc/init.d/
路徑下,或者創建需要的腳本文件即可
之后增加可執行權限
chmod +x /etc/init.d/scriptname.sh
最后添加自啟動
chkconfig --add scriptname.sh
chkconfig scriptname.sh on
最后一步是不是可以用systemctl enable scripname.sh
我沒試過
查到還有一種方法是先在/etc/profile.d/
創建腳本之后添加可執行權限再拷貝至/etc/init.d/
,之后的流程相同。
參數
chkconfig方法的參數設置
#!/bin/sh
表明此腳本使用/bin/sh來解釋執行
#chkconfig: 2345 80 90
2345表示系統運行級別
0——關機,
1——單用戶,就是我們之前修改root賬戶密碼的模式,
2——多用戶模式,但比3模式少了一個nfs服務
3——多用戶命令行模式,最常用
4——保留級別暫時沒用,
5——圖形模式,
6——重啟
80表示啟動優先級
90表示關閉優先級
#description:xxxxxxxxx
腳本的描述信息
systemctl方法的參數設置(以nginx為例)
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]服務的說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設置
Type=forking是后台運行的形式
ExecStart為服務的具體運行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啟動、重啟、停止命令全部要求使用絕對路徑。[Install]運行級別下服務安裝的相關設置,可設置為多用戶,即系統運行級別為3。
參考資料
https://www.liangzl.com/get-article-detail-15933.html
https://baijiahao.baidu.com/s?id=1593743216505421871&wfr=spider&for=pc
https://www.cnblogs.com/startcentos/p/6147444.html
https://blog.csdn.net/abcwanglinyong/article/details/84638125