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.local
文件中添加啓動命令html
在CentOS7以後可能沒法自啓動nginx
CentOS7以後採用systemd做爲init,rc.local
也是經過systemd
的service啓動的。redis
輸入vim
cat /usr/lib/systemd/system/rc-local.service
查看其啓動配置centos
# 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
必須得有執行權限。網絡
若其沒有執行權限,應給予。socket
chmod +x /etc/rc.d/rc.local
以後則須要啓動rc-local.service
服務ide
systemctl enable rc-local.service systemctl start rc-local.service
若未能成功啓動ui
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啓動就能夠了。this
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
此方法貌似是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