linux添加開機啓動腳本 原

說明

0——關機, 1——單用戶,就是我們之前修改root賬戶密碼的模式, 2——多用戶模式,但比3模式少了一個nfs服務 3——多用戶命令行模式,最常用 4——保留級別暫時沒用, 5——圖形模式, 6——重啓

一、chkconfig-CentOS6以前

chkconfig就是CentOS6以前用來控制系統服務的工具,系統開機時啓動的部分服務存儲在/etc/init.d/目錄下。我們可以把需要開機啓動的服務放在這個目錄下然後用chkconfig來管理。

查看命令

chkconfig --list #列出所有的系統服務。

操作命令

chkconfig --add httpd #增加httpd服務。
chkconfig --del httpd #刪除httpd服務。
chkconfig --level httpd 2345 on #設置httpd在運行級別爲2、3、4、5的情況下都是on(開啓)的狀態。
chkconfig --list mysqld #列出mysqld服務設置情況。
chkconfig --level 35 mysqld on #設定mysqld在等級3和5爲開機運行服務,--level 35表示操作只在等級3和5執行,on表示啓動,off表

示例

chkconfig --add nginx #添加nginx服務開機啓動項

創建命令

(略)

二、systemd-CentOS6以後

查看命令

systemctl list-units --all --type=service #查看所有服務
systemctl list-units --type=service #查看所有已經啓動的服務

操作命令

針對單一服務的

systemctl enable crond ##設置開機啓動crond服務或工具
systemctl disable crond ##設置關閉開機啓動crond服務或工具
systemctl status crond ##查看crond服務當前狀態,如是否運行
systemctl stop crond ##停止crond服務是,但開機仍會運行
systemctl start crond ##開啓crond服務
systemctl restart crond ##重啓crond服務
systemctl is-enabled crond ##檢查crond服務是否開機啓動

示例:

systemctl enable nginx.service #添加nginx服務開機啓動項

創建命令

vim /lib/systemd/system/nginx.service #在系統服務目錄裏創建nginx.service文件

內容:

[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

保存退出。

systemctl enable nginx.service #設置開機啓動

三、chkconfig 和systemctl 對比

任            務

舊         指        令

新        指      令

使某服務自動啓動

chkconfig –level 3 httpd on

systemctl enable httpd.service

使某服務不自動啓動

chkconfig –level 3 httpd off

systemctl disable httpd.service

檢查服務狀態

service httpd status

systemctl status httpd.service

顯示所有已啓動的服務

chkconfig –list

systemctl list-units –type=service

啓動某服務

service httpd start

systemctl start httpd.service

停止某服務

service httpd stop

systemctl stop httpd.service

重啓某服務

service httpd restart

systemctl restart httpd.service

四、對照表

1.Systemd 命令和 sysvinit 命令的對照表

2.Sysvinit 運行級別和 systemd 目標的對應表

參考文檔

1.Linux系統管理初步(七)系統服務管理、chkconfig與systemd 編輯中:https://www.cnblogs.com/superlinux/p/bfd4812adffaccb36520279aaafcc160.html

2.Nginx+Center OS 7.2 開機啓動設置:https://www.cnblogs.com/piscesLoveCc/p/5867900.html

3.Linux 設置程序開機自啓動 (命令systemctl 和 chkconfig用法區別比較):https://blog.csdn.net/kenhins/article/details/74518978

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章