1. 程式人生 > >CentOS7管理系統服務命令systemd

CentOS7管理系統服務命令systemd

centos7管理系統服務命令systemd

  1. 首先系統啟動流程:


    POST --> Boot Sequeue(BIOS) --> Bootloader(MBR) --> Kernel(ramdisk) --> rootfs --> /sbin/init

    init:

    CentOS 5: SysV init

    CentOS 6:Upstart

    CentOS 7:Systemd


    Systemd的新特性:

    系統引導時實現服務並行啟動;

    按需激活進程;

    系統狀態快照;

    基於依賴關系定義服務控制邏輯

  2. 核心概念:unit

    unit由其相關配置文件進行標識、識別和配置;文件中主要包含了系統服務、監聽的socket、保存的快照以及其它與init相關的信息; 這些配置文件主要保存在:

    /usr/lib/systemd/system

    /run/systemd/system

    /etc/systemd/system

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: .path, 用於定義文件系統中的一文件或目錄;

關鍵特性:

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

基於bus的激活機制;

基於device的激活機制;

基於Path的激活機制;

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

向後兼容sysv init腳本;

/etc/init.d/

不兼容:

systemctl的命令是固定不變的;

非由systemd啟動的服務,systemctl無法與之通信;

3. 管理系統服務:

CentOS 7: service類型的unit文件;

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


4. 管理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

5. 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


本文出自 “11290766” 博客,請務必保留此出處http://rylan.blog.51cto.com/11290766/1930471

CentOS7管理系統服務命令systemd