歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> CentOS 6,7系統服務管理

CentOS 6,7系統服務管理

日期:2017/3/6 9:50:17   编辑:學習Linux

CentOS 6,7系統服務管理


CentOS 6,7系統服務管理


1、CentOS 5 使用sysV風格,即串行模式

2、CentOS 6 使用upstart,並發模式

3、CentOS 7 使用systemd,並發模式

systemV 風格特點:系統服務啟動不能自動解決依賴關系,由此,依據/etc/rc.d/下的運行級別來啟動各個服務。

systemd並發模式可以自動解決依賴關系,服務啟動時自動檢查。按服務的功能將服務分成各個unit。

常見的類型有:

Service unit:文件擴展名為.service,用於定義系統服務;

Target unit:文件擴展為.target,用於模擬實現“運行級別”;

Device unit: .device,用於定義內核識別的設備;

Mount unit: .mount,定義文件系統掛載點;

Socket unit: .socket,用於標識進程間通信用到的socket文件;

Snapshot unit: .snapshot, 管理系統快照;

Swap unit: .swap, 用於標識swap設備;

Automount unit: .automount,文件系統自動點設備;

Path unit:

關鍵特性:

基於socket的激活機制:socket與程序分離;

基於bus的激活機制;

基於device的激活機制;

基於Path的激活機制;

系統快照:保存各unit的當前狀態信息於持久存儲設備中;

向後兼容sysv init腳本;

/etc/init.d/

官方釋放出將配置文件放置在/usr/lib/systemd/system目錄下,redhat將此目錄下的所有文件連接到/etc/systemd/system,建議修改這個文件。

注意所有文件都是放置在上面的目錄中才能通過systemctl命令來管理。所以一般源碼安裝等需要注意一些問題。

syscemctl命令:

- Control the systemd system and service manager

systemctl [OPTIONS...] COMMAND [NAME...]

啟動: service NAME start ==> systemctl start NAME.service

停止: service NAME stop ==> systemctl stop NAME.service

重啟: service NAME restart ==> systemctl restart NAME.service

狀態: service NAME status ==> systemctl status NAME.service

條件式重啟:service NAME condrestart ==> systemctl try-restart NAME.service

重載或重啟服務: systemctl reload-or-restart NAME.servcie

重載或條件式重啟服務:systemctl reload-or-try-restart NAME.service

查看某服務當前激活與否的狀態: systemctl is-active NAME.service

查看所有已激活的服務:systemctl list-units --type service

查看所有服務(已激活及未激活): chkconfig --lsit ==> systemctl list-units -t service --all

設置服務開機自啟: chkconfig NAME on ==> systemctl enable NAME.service

禁止服務開機自啟: chkconfig NAME off ==> systemctl disable NAME.service

查看某服務是否能開機自啟: chkconfig --list NAME ==> systemctl is-enabled NAME.service

禁止某服務設定為開機自啟: systemctl mask NAME.service

取消此禁止: systemctl unmask NAME.servcie

查看服務的依賴關系:systemctl list-dependencies NAME.service

管理target units:

運行級別:

0 ==> runlevel0.target, poweroff.target

1 ==> runlevel1.target, rescue.target

2 ==> runlevel2.tartet, multi-user.target

3 ==> runlevel3.tartet, multi-user.target

4 ==> runlevel4.tartet, multi-user.target

5 ==> runlevel5.target, graphical.target

6 ==> runlevel6.target, reboot.target

級別切換: init N ==> systemctl isolate NAME.target

查看級別: runlevel ==> systemctl list-units --type target

查看所有級別: systemctl list-units -t target -a

獲取默認運行級別:systemctl get-default

修改默認運行級別: systemctl set-default NAME.target

切換至緊急救援模式: systemctl rescue

切換至emergency模式: systemctl emergency

其它常用命令:

關機: systemctl halt, systemctl poweroff

重啟: systemctl reboot

掛起: systemctl suspend

快照: systemctl hibernate

快照並掛起: systemctl hybrid-sleep

service unit file:

文件通常由三部分組成:

[Unit]:定義與Unit類型無關的通用選項;用於提供unit的描述信息、unit行為及依賴關系等;

[Service]:與特定類型相關的專用選項;此處為Service類型;

[Install]:定義由“systemctl enable”以及"systemctl disable“命令在實現服務啟用或禁用時用到的一些選項;

Unit段的常用選項:

Description:描述信息; 意義性描述;

After:定義unit的啟動次序;表示當前unit應該晚於哪些unit啟動;其功能與Before相反;

Requies:依賴到的其它units;強依賴,被依賴的units無法激活時,當前unit即無法激活;

Wants:依賴到的其它units;弱依賴;

Conflicts:定義units間的沖突關系;

Service段的常用選項:

Type:用於定義影響ExecStart及相關參數的功能的unit進程啟動類型;

類型:

simple:

forking:

oneshot:

dbus:

notify:

idle:

EnvironmentFile:環境配置文件;

ExecStart:指明啟動unit要運行命令或腳本; ExecStartPre, ExecStartPost

ExecStop:指明停止unit要運行的命令或腳本;

Restart:

Install段的常用選項:

Alias:

RequiredBy:被哪些units所依賴;

WantedBy:被哪些units所依賴;

注意:對於新創建的unit文件或,修改了的unit文件,要通知systemd重載此配置文件;

# systemctl daemon-reload

http://xxxxxx/Linuxjc/1134102.html TechArticle

Copyright © Linux教程網 All Rights Reserved