天天看點

centos7 中 systemd systemctl管理服務的指令

背景: centos7 和 6的重大改變

對于系統管理者而言,系統的服務管理是一件很日常和很重要的工作,而7在6的基礎上有了很大的改變,就連指令都完全不一樣了。是以要擁抱變化,學習7是如何進行服務的管理和控制的。

system和systemctl的初探

Systemctl是一個systemd工具,主要負責控制systemd系統和服務管理器。

Systemd是一個系統管理守護程序、工具和庫的集合,用于取代System V初始程序。Systemd的功能是用于集中管理和配置類UNIX系統。

在Linux生态系統中,Systemd被部署到了大多數的标準Linux發行版中,隻有為數不多的幾個發行版尚未部署。Systemd通常是所有其它守護程序的父程序,但并非總是如此。

【第一步】

檢視systemd的版本

# systemctl --version

systemd 219

+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

檢視程序是否存在

ps -ef | grep [s]ystemd

注意:systemd是作為父程序(pid=1)運作的

分析systemd的啟動程序

# systemd-analyze

Startup finished in 639ms (kernel) + 931ms (initrd) + 12.432s (userspace) = 14.003s

分析各個程序啟動發費的時間

#systemd-analyze blame

分析啟動時候的關鍵鍊

systemd-analyze critical-chain

重要:Systemctl接受服務(.service),挂載點(.mount),套接口(.socket)和裝置(.device)作為單元。

列出所有服務可用單元

systemctl list-unit-files

(200多個)

列出所有運作中的單元

systemctl list-units

列出失敗的單元?

systemctl --failed

列出某個單元是否啟動

# systemctl is-enabled crond.service

enabled

或者

# systemctl is-enabled crond

檢查某個單元或服務是否運作

# systemctl is-active crond 

active

# systemctl status crond   

這個資訊更詳細

############################# 控制服務  ############################

列出所有服務(包括啟用的和禁用的)

systemctl list-unit-files  --type=service

(120+)

以httpd為例

yum install httpd

會生成以下檔案

/usr/lib/systemd/system/httpd.service 

Linux中如何啟動、重新開機、停止、重載服務以及檢查服務(如 httpd.service)狀态

[root@Centos7-node2 ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server

   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

   Active: inactive (dead)

     Docs: man:httpd(8)

           man:apachectl(8)

[root@Centos7-node2 ~]# systemctl start httpd 

   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

   Active: active (running) since Sun 2016-10-23 00:48:43 CST; 3s ago

 Main PID: 26811 (httpd)

   Status: "Processing requests..."

   CGroup: /system.slice/httpd.service

           ├─26811 /usr/sbin/httpd -DFOREGROUND

           ├─26812 /usr/sbin/httpd -DFOREGROUND

           ├─26813 /usr/sbin/httpd -DFOREGROUND

           ├─26814 /usr/sbin/httpd -DFOREGROUND

           ├─26815 /usr/sbin/httpd -DFOREGROUND

           └─26816 /usr/sbin/httpd -DFOREGROUND

Oct 23 00:48:17 Centos7-node2 systemd[1]: Starting The Apache HTTP Server...

Oct 23 00:48:33 Centos7-node2 httpd[26811]: AH00558: httpd: Could not reliably determine the server's fully qua...ssage

Oct 23 00:48:43 Centos7-node2 systemd[1]: Started The Apache HTTP Server.

Hint: Some lines were ellipsized, use -l to show in full.

[root@Centos7-node2 ~]# systemctl reload httpd

[root@Centos7-node2 ~]# systemctl stop httpd  

注意:

Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

在loader中第一個disabled表示系統啟動是否自啟動,為非自動。 第二個不知道幹嘛

Active: active (running) since Sun 2016-10-23 00:48:43 CST; 3s ago

Active 表示服務是不是在運作

如何激活服務并在啟動時啟用或禁用服務(即系統啟動時自動啟動服務)

[root@Centos7-node2 ~]# systemctl enable httpd

Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

禁用

systemctl disabled httpd

[root@Centos7-node2 ~]# systemctl disable httpd 

Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.

使用systemctl指令殺死服務

[root@Centos7-node2 ~]# systemctl kill httpd

   Active: failed (Result: exit-code) since Sun 2016-10-23 00:56:59 CST; 1s ago

  Process: 26901 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)

 Main PID: 26869 (code=exited, status=0/SUCCESS)

   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"

Oct 23 00:51:38 Centos7-node2 systemd[1]: Starting The Apache HTTP Server...

Oct 23 00:51:53 Centos7-node2 httpd[26869]: AH00557: httpd: apr_sockaddr_info_get() failed for Centos7-node2

Oct 23 00:51:53 Centos7-node2 httpd[26869]: AH00558: httpd: Could not reliably determine the server's fully qua...ssage

Oct 23 00:52:03 Centos7-node2 systemd[1]: Started The Apache HTTP Server.

Oct 23 00:56:59 Centos7-node2 kill[26901]: kill: cannot find process ""

Oct 23 00:56:59 Centos7-node2 systemd[1]: httpd.service: control process exited, code=exited status=1

Oct 23 00:56:59 Centos7-node2 systemd[1]: Unit httpd.service entered failed state.

Oct 23 00:56:59 Centos7-node2 systemd[1]: httpd.service failed.

注意:Active: failed (Result: exit-code) since Sun 2016-10-23 00:56:59 CST; 1s ago

這個的failed表示的是kill掉的?

##########################  使用Systemctl控制并管理挂載點 #################3

[root@Centos7-node2 ~]# systemctl list-unit-files --type=mount

UNIT FILE                     STATE   

dev-hugepages.mount           static  

dev-mqueue.mount              static  

proc-sys-fs-binfmt_misc.mount static  

sys-fs-fuse-connections.mount static  

sys-kernel-config.mount       static  

sys-kernel-debug.mount        static  

tmp.mount                     disabled

挂載、解除安裝、重新挂載、重載系統挂載點并檢查系統中挂載點狀态

systemctl start tmp.mount

systemctl status tmp.mount

systemctl reload tmp.mount

在啟動時激活、啟用或禁用挂載點(系統啟動時自動挂載)

# systemctl is-active tmp.mount

# systemctl enable tmp.mount

# systemctl disable tmp.mount

在Linux中屏蔽(讓它不能啟用)或可見挂載點

# systemctl mask tmp.mount

ln -s '/dev/null''/etc/systemd/system/tmp.mount'

# systemctl unmask tmp.mount

rm '/etc/systemd/system/tmp.mount'

####################  控制系統運作等級  ##################

啟動系統救援模式

# systemctl rescue

Broadcast message from root@tecmint on pts/0(Wed2015-04-2911:31:18 IST):

The system is going down to rescue mode NOW!

進入緊急模式

# systemctl emergency   

Welcome to emergency mode!After logging in, type "journalctl -xb" to view

system logs,"systemctl reboot" to reboot,"systemctl default" to try again

to boot intodefault mode.

列出目前使用的運作等級

注意:init 1也是可以使用的

# systemctl get-default

multi-user.target

注意:who -r 也是可以檢視的

啟動運作等級5,即圖形模式

# systemctl isolate runlevel5.target

# systemctl isolate graphical.target

啟動運作等級3,即多使用者模式(指令行)

# systemctl isolate runlevel3.target

# systemctl isolate multiuser.target

設定多使用者模式或圖形模式為預設運作等級

# systemctl set-default runlevel3.target

# systemctl set-default runlevel5.target

重新開機、停止、挂起、休眠系統或使系統進入混合睡眠

# systemctl reboot             

# systemctl halt

# systemctl suspend

# systemctl hibernate

# systemctl hybrid-sleep

對于不知運作等級為何物的人,說明如下。

Runlevel 0 : 關閉系統

Runlevel 1 : 救援?維護模式

Runlevel 3 : 多使用者,無圖形系統

Runlevel 4 : 多使用者,無圖形系統

Runlevel 5 : 多使用者,圖形化系統

Runlevel 6 : 關閉并重新開機機器

注意:在centos7 中仍然可以使用init 0 關機 init 6 啟動。   

本文轉自殘劍部落格51CTO部落格,原文連結http://blog.51cto.com/cuidehua/1864579如需轉載請自行聯系原作者

cuizhiliang