第五章:系統服務

第五章:系統服務

1.服務目錄

 

在之前的Redhat Linux5.x&6.x的系統中,我們知道,系統的服務都是存放在/etc/init.d這個目錄中的,但是在RHEL7.x的系統中,已經發生了改變,如下:

------------------------------------------查看RHEL7之前的系統服務路徑------------------------------

[root@ freeit ~]# cd /etc/init.d/

[root@ freeit init.d]# ls

abrt-ccpp         iscsi           restorecond

abrtd             iscsid          rhnsd

abrt-oops         kdump           rhsmcertd

acpid             killall         rngd

atd               libvirt-guests  rpcbind

auditd            lldpad          rpcgssd

autofs            lvm2-lvmetad    rpcidmapd

blk-availability  lvm2-monitor    rpcsvcgssd

bluetooth         mdmonitor       rsyslog

certmonger        messagebus      sandbox

cgconfig          multipathd      saslauthd

cgred             mysqld          single

cpuspeed          named           smartd

crond             netconsole      snmpd

cups              netfs           snmptrapd

dhcpd             network         spice-vdagentd

dhcpd6            NetworkManager  sshd

dhcrelay          nfs             sssd

dnsmasq           nfslock         sysstat

fcoe              ntpd            udev-post

firstboot         ntpdate         vmware-tools

functions         oddjobd         vmware-tools-thinprint

haldaemon         openct          vncserver

halt              pcscd           wdaemon

htcacheclean      portreserve     winbind

httpd             postfix         wpa_supplicant

ip6tables         psacct          xinetd

iptables          quota_nld       ypbind

irqbalance        rdisc

-----------------------------------------查看RHEL7/etc/init.d/目錄----------------------------------

[root@ freeit tmp]# cd /etc/init.d/

[root@ freeit init.d]# ls

functions   iprinit    netconsole  README

iprdump     iprupdate  network     rhnsd

//如上,此目錄不再是系統服務的所在路徑,所以,重啓服務也不能使用/etc/init.d servername  restart來啓動

 

 

 

2.服務下次啓動狀態

 

RHEL7.x之前的系統中,使用命令chkconfig –list即可查看所有服務的下次啓動狀態,但是在RHEL7中,此命令已不可用

[root@ freeit ~]# chkconfig --list

 

Note: This output shows SysV services  only and does not include native

       systemd services. SysV configuration data might be overridden by  native

       systemd configuration.

 

       If you want to list systemd services use 'systemctl list-unit-files'.

       To see services enabled on particular target use

       'systemctl list-dependencies [target]'.

 

iprdump         0:off   1:off    2:on    3:on    4:on     5:on    6:off

iprinit         0:off   1:off    2:on    3:on    4:on     5:on    6:off

iprupdate       0:off    1:off   2:on    3:on     4:on    5:on    6:off

netconsole      0:off    1:off   2:off   3:off    4:off   5:off   6:off

network         0:off   1:off    2:on    3:on    4:on     5:on    6:off

rhnsd           0:off   1:off    2:on    3:on    4:on     5:on    6:off

//此命令列出的只是一些基本的服務。要想查看所有的,根據提示命令查看(加粗字體)

---------------------------------------------systemctl list-unit-files---------------------------------------------

[root@ freeit ~]# systemctl  list-unit-files

UNIT FILE                                   STATE  

proc-sys-fs-binfmt_misc.automount           static   

dev-hugepages.mount                         static 

dev-mqueue.mount                            static 

proc-fs-nfsd.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

var-lib-nfs-rpc_pipefs.mount                static 

brandbot.path                               disabled

cups.path                                   enabled

systemd-ask-password-console.path           static 

systemd-ask-password-plymouth.path          static 

………………………..

 

 

 

3.系統運行級別

 

RHEL7中,系統的運行級別已沒有0-6之說。運行級別的inittab文件也不再包含0-6級別的說明。如下:

[root@ freeit ~]# vim /etc/inittab

# inittab is no longer used when using  systemd.

#

# ADDING CONFIGURATION HERE WILL HAVE NO  EFFECT ON YOUR SYSTEM.

#

# Ctrl-Alt-Delete is handled by  /etc/systemd/system/ctrl-alt-del.targe

t

#

# systemd uses 'targets' instead of  runlevels. By default, there are t

wo main targets:

#

#  multi-user.target: analogous to runlevel 3

//相當於運行級別3,命令行界面

#  graphical.target: analogous to runlevel 5

//相當於運行級別5,圖形化界面

#

# To set a default target, run:

#

# ln -sf /lib/systemd/system/<target  name>.target /etc/systemd/system/

default.target

#

~                                                                

 

查看系統缺省運行級別

[root@ freeit ~]# systemctl  get-default

graphical.target

//默認運行級別爲圖形化界面

 

設置默認運行級別

[root@ freeit ~]#systemctl set-default  multi-user.target

[root@ freeit ~]# systemctl get-default

multi-user.target

 

查看設置的運行級別的服務狀態(下次啓動狀態)

[root@ freeit ~]# systemctl  list-unit-files |grep multi-user

multi-user.target                           enabled

 

 

 

 

4.服務下次啓動狀態

 

我們知道,服務啓動之後,要想下次重啓生效,RHEL7之前,要使用命令chkconfig servername on,但是在RHEL7中,已不能使用此命令。那麼要如何設置,看下面的例子:

[root@ freeit ~]# systemctl restart  postfix.service

//啓動郵件服務

[root@ freeit ~]# systemctl status  postfix.service

//查看郵件服務的運行狀態

postfix.service - Postfix Mail Transport  Agent

    Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled)

//下次啓動爲開啓

    Active: active (running) since Mon 2015-05-04 11:03:42 CST; 14s  ago

//當前正在運行

   Process: 4611 ExecStop=/usr/sbin/postfix stop (code=exited,  status=0/SUCCESS)

  Process: 4626 ExecStart=/usr/sbin/postfix  start (code=exited, status=0/SUCCESS)

   Process: 4623 ExecStartPre=/usr/libexec/postfix/chroot-update  (code=exited, status=0/SUCCESS)

   Process: 4621 ExecStartPre=/usr/libexec/postfix/aliasesdb  (code=exited, status=0/SUCCESS)

 Main PID: 4698 (master)

    CGroup: /system.slice/postfix.service

            ├─4698 /usr/libexec/postfix/master –w

            ├─4699 pickup -l -t unix -u

            └─4700 qmgr -l -t unix -u

 

May 04 11:03:42 localhost.localdomain  systemd[1]: Starting Postfix Ma...

May 04 11:03:42 localhost.localdomain  postfix/master[4698]: daemon st...

May 04 11:03:42 localhost.localdomain  systemd[1]: Started Postfix Mai...

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

 

------------------------------------------------關閉下次開機啓動-------------------------------------------

[root@ freeit ~]# systemctl disable  postfix.service

rm  '/etc/systemd/system/multi-user.target.wants/postfix.service'

[root@ freeit ~]# systemctl status  postfix.service 

postfix.service - Postfix Mail Transport  Agent

    Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled)

    Active: active (running) since Mon  2015-05-04 11:03:42 CST; 4min 30s ago

 Main PID: 4698 (master)

    CGroup: /system.slice/postfix.service

            ├─4698 /usr/libexec/postfix/master -w

            ├─4699 pickup -l -t unix -u

            └─4700 qmgr -l -t unix -u

 

May 04 11:03:42 localhost.localdomain  systemd[1]: Starting Postfix Ma...

May 04 11:03:42 localhost.localdomain  postfix/master[4698]: daemon st...

May 04 11:03:42 localhost.localdomain  systemd[1]: Started Postfix Mai...

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

//下次重啓生效功能關閉之後,重啓系統,郵件服務即爲關閉狀態

 

關閉之後重啓驗證

[root@ freeit ~]# systemctl status  postfix.service

postfix.service - Postfix Mail Transport  Agent

    Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled)

    Active: inactive (dead)

//沒有啓動

 

 

重新啓動郵件服務,並設置下次開機啓動

 [root@ freeit ~]# systemctl restart postfix

[root@localhost ~]# systemctl enable  postfix.service

ln -s '/usr/lib/systemd/system/postfix.service'  '/etc/systemd/system/multi-user.target.wants/postfix.service'

[root@ freeit ~]# systemctl status  postfix.service

postfix.service - Postfix Mail Transport  Agent

    Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled)

    Active: active (running) since Mon 2015-05-04 11:12:14 CST; 35s  ago

 Main PID: 2994 (master)

    CGroup: /system.slice/postfix.service

            ├─2994 /usr/libexec/postfix/master -w

            ├─2995 pickup -l -t unix -u

            └─2996 qmgr -l -t unix -u

 

May 04 11:12:14 localhost.localdomain  systemd[1]: Starting Postfix Ma...

May 04 11:12:14 localhost.localdomain  postfix/master[2994]: daemon st...

May 04 11:12:14 localhost.localdomain  systemd[1]: Started Postfix Mai...

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

 

 

 

5.服務狀態相關命令

 

  • 查看當前啓動狀態

[root@ freeit ~]# systemctl is-active  postfix.service

Active

//當前正在運行

 

  • 關閉當前運行狀態,再次查看

[root@ freeit ~]# systemctl stop  postfix.service

[root@ freeit ~]# systemctl is-active  postfix.service

inactive

 

  • 查看下次開機啓動狀態

[root@ freeit ~]# systemctl is-enabled  postfix.service

enabled

//下次開機爲啓動

 

  • 關閉下次開機啓動,並再次查看

[root@ freeit ~]# systemctl disable  postfix.service

rm '/etc/systemd/system/multi-user.target.wants/postfix.service'

[root@localhost ~]# systemctl is-enabled  postfix.service

disabled

 

 

 

6.鎖定服務(mask)

 

1:對postfix服務執行mask操作,然後執行重啓操作

[root@ freeit ~]# systemctl mask postfix

ln -s '/dev/null' '/etc/systemd/system/postfix.service'

[root@localhost ~]# systemctl restart  postfix

Failed to issue method call: Unit  postfix.service is masked.

//重啓服務失敗

[root@ freeit ~]# systemctl status  postfix

postfix.service

    Loaded: masked (/dev/null)

    Active: inactive (dead)

 

May 04 11:12:14 localhost.localdomain  systemd[1]: Starting Postfix Ma...

May 04 11:12:14 localhost.localdomain  postfix/master[2994]: daemon st...

May 04 11:12:14 localhost.localdomain  systemd[1]: Started Postfix Mai...

May 04 11:18:39 localhost.localdomain  systemd[1]: Stopping Postfix Ma...

May 04 11:18:39 localhost.localdomain  systemd[1]: Stopped Postfix Mai...

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

 

2:解鎖服務並再次重啓

[root@ freeit ~]# systemctl unmask  postfix

//解鎖

rm '/etc/systemd/system/postfix.service'

[root@ freeit ~]# systemctl restart  postfix

//重啓成功

[root@ freeit ~]# systemctl status  postfix 

postfix.service - Postfix Mail Transport  Agent

    Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled)

    Active: active (running) since Mon 2015-05-04 11:29:36 CST; 3s  ago

   Process: 3529 ExecStart=/usr/sbin/postfix start (code=exited,  status=0/SUCCESS)

   Process: 3527 ExecStartPre=/usr/libexec/postfix/chroot-update  (code=exited, status=0/SUCCESS)

   Process: 3524 ExecStartPre=/usr/libexec/postfix/aliasesdb  (code=exited, status=0/SUCCESS)

 Main PID: 3601 (master)

    CGroup: /system.slice/postfix.service

            ├─3601 /usr/libexec/postfix/master -w

            ├─3602 pickup -l -t unix -u

            └─3603 qmgr -l -t unix -u

 

May 04 11:29:36 localhost.localdomain  postfix/master[3601]: daemon st...

May 04 11:29:36 localhost.localdomain  systemd[1]: Started Postfix Mai...

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

 

 

7.系統進程

 

 

7.1.實時查看系統進程

 

top  - 11:34:23 up 25 min,  1 user,  load average: 0.00, 0.01, 0.05

Tasks:  297 total,   4 running, 293  sleeping,   0 stopped,   0 zombie

%Cpu(s):  0.0 us,   0.0 sy,  0.0 ni, 99.7 id,  0.0 wa,   0.0 hi,  0.3 si,  0.0 st

KiB  Mem:   1010788 total,   306908 used,   703880 free,     1080 buffers

KiB  Swap:  2097148 total,        0 used,  2097148 free.   110788 cached Mem

 

  PID USER      PR   NI    VIRT    RES     SHR S %CPU %MEM     TIME+  COMMAND                  

 3651 root      20    0  123788   1772    1148 R  0.3  0.2    0:00.03 top                        

     1 root      20   0    53948   7808   2548 S   0.0  0.8   0:05.08 systemd                  

     2 root      20   0        0      0      0 S   0.0  0.0   0:00.00 kthreadd                 

     3 root      20   0        0      0       0 R  0.0  0.0    0:00.04 ksoftirqd/0                

     5 root       0 -20       0       0      0 S  0.0   0.0   0:00.00 kworker/0:0H             

     7 root      rt   0        0      0      0 S   0.0  0.0   0:00.00 migration/0              

8 root      20    0       0      0       0 S  0.0  0.0    0:00.00 rcu_bh     

…………………..

--------------------------------------------更詳細查看,按1-------------------------------------------------

 top - 11:35:00 up 25 min,  1 user,   load average: 0.00, 0.01, 0.05

Tasks: 297 total,   2 running, 295 sleeping,   0 stopped,   0 zombie

%Cpu0  :   0.7 us,  0.7 sy,  0.0 ni, 98.7 id,  0.0 wa,   0.0 hi,  0.0 si,  0.0 st

KiB Mem:    1010788 total,   307172  used,   703616 free,     1080 buffers

KiB Swap:   2097148 total,        0  used,  2097148 free.   110788 cached Mem

 

   PID USER      PR  NI     VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                  

   879 root      20   0   193424   4168   3344 S   1.0  0.4   0:02.80 vmtoolsd                 

 3651 root      20    0  123788   1772    1148 R  0.7  0.2    0:00.20 top                        

     1 root      20   0    53948   7808   2548 S   0.0  0.8   0:05.08 systemd                  

     2 root      20   0        0      0      0 S   0.0  0.0   0:00.00 kthreadd                 

     3 root      20   0        0      0      0 S   0.0  0.0   0:00.04 ksoftirqd/0              

     5 root       0 -20       0       0      0 S  0.0   0.0   0:00.00 kworker/0:0H             

     7 root      rt   0        0      0      0  S  0.0   0.0   0:00.00 migration/0              

     8 root      20   0        0      0      0 S   0.0  0.0   0:00.00 rcu_bh      

q鍵退出進程

 

  • 5行說明:

5行是系統整體的統計信息

  • 第一行是任務隊列信息,同uptime(查看服務器啓動時間)命令的執行結果。內容如下:

  • 11:34:23:當前時間;

  • up  1:13:系統運行時間,格式爲時:分;

  • 1 user:當前登錄用戶數;

  • load average: 0.00, 0.01, 0.05:系統負載,即任務隊列的平均長度。三個數值分別爲1分鐘、5分鐘、15分鐘前到現在的平均值。

  • 第二、三行爲進程和CPU的信息。當有多個CPU時,這些內容可能會超過兩行,內容如下:

  • Tasks: 297 total:進程總數;

  • 4 running:正在運行的進程數;

  • 293 sleeping:睡眠的進程數;

  • 0 stopped:停止進程數;

  • 0 zombie:殭屍進程;

  • %Cpu(s):  0.0 us:用戶空間佔用CPU百分比;

  • 0.0 sy:內核空間佔用CPU百分比; 

  • 0.0 ni:用戶進程空間內改變過優先級的進程佔用CPU百分比;

  • 99.7 idid空閒CPU百分比; 

  • 0.0 wawa等待輸入輸出的CPU時間百分比 

  • 0.0 hi  0.3 si, 0.0 st

  • 最後兩行爲內存信息。內容如下:

  • KiB Mem:   1010788 total:物理內存總量

  • 306908 used:使用的物理內存總量;

  • 703880 free:空閒的物理內存總量;

  • 1080 buffers:用作內核緩存的物理內存量;

  • KiB Swap:  2097148 total:交換區總量;

  • 0 used:使用的交換分區總量;

  • 2097148 free:空閒的交換區總量;

  • 110788 cached Mem:緩衝的交換區總量。

 

  • 進程區信息說明:

  • PID:進程ID

  • USER:進程所有者;

  • PR:進程優先級;

  • NInice值。負值表示高優先級;正值表示低優先級;

  • VIRT:進程使用的虛擬內存總量,單位KbVITR=SWAP+RES

  • RES:進程使用的、未被交換出的物理內存大小,單位KBRES=CODE+DATA

  • SHR:共享內存大小,單位Kb

  • S:進程狀態;

  • %CPU:上次更新到現在的CPU時間戰用百分比;

  • %MEM:進程使用的物理內存百分比;

  • TIME+:進程使用的CPU時間總計,單位1/100秒;

  • COMMAND:命令名/命令行

 

 

7.2.靜態查看當前進程狀態

 

[root@ freeit ~]# ps aux

USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root         1   0.3  0.7  53948   7808 ?        Ss   11:09    0:05 /usr/lib/systemd/systemd --s

root         2   0.0  0.0      0      0 ?        S    11:09    0:00 [kthreadd]

root         3   0.0  0.0      0      0 ?        S    11:09    0:00 [ksoftirqd/0]

root         5   0.0  0.0      0      0 ?        S<   11:09    0:00 [kworker/0:0H]

root         7   0.0  0.0      0      0 ?        S    11:09    0:00 [migration/0]

root         8   0.0  0.0      0      0 ?        S    11:09    0:00 [rcu_bh]

root         9   0.0  0.0      0      0 ?        S    11:09    0:00 [rcuob/0]

root        10   0.0  0.0      0      0 ?        S    11:09    0:00 [rcuob/1]

root        11   0.0  0.0      0      0 ?        S    11:09    0:00 [rcuob/2]

root        12   0.0  0.0      0      0 ?        S    11:09    0:00 [rcuob/3]

………………………….

常用參數:

     -a:包含所有終端的進程

     -u:顯示進程的擁有者

     -x:顯示不屬於任何終端的進程

     -f:顯示進程的父子關係

 

 

7.3.查看進程樹

 

[root@ freeit ~]# pstree

systemd─┬─ModemManager───2*[{ModemManager}]

         ├─NetworkManager───2*[{NetworkManager}]

         ├─2*[abrt-watch-log]

         ├─abrtd

         ├─agetty

         ├─alsactl

         ├─atd

         ├─auditd─┬─audispd─┬─sedispatch

                          └─{audispd}

                 └─{auditd}

         ├─avahi-daemon───avahi-daemon

         ├─bluetoothd

         ├─chronyd

         ├─crond

         ├─dbus-daemon───{dbus-daemon}

         ├─firewalld───{firewalld}

         ├─iprdump

         ├─iprinit

         ├─iprupdate

         ├─ksmtuned───sleep

         ├─libvirtd───10*[{libvirtd}]

         ├─lsmd

         ├─lvmetad

         ├─master─┬─pickup

                 └─qmgr

         ├─packagekitd───2*[{packagekitd}]

         ├─polkitd───5*[{polkitd}]

         ├─rhsmcertd

         ├─rngd

         ├─rpc.statd

         ├─rpcbind

         ├─rsyslogd───2*[{rsyslogd}]

         ├─smartd

         ├─sshd───sshd───bash───pstree

         ├─systemd-journal

         ├─systemd-logind

         ├─systemd-udevd

         ├─tuned───4*[{tuned}]

         └─vmtoolsd

 

 

 

8.前臺後臺(fg&bg)

 

例:把/usr複製到/tmp下,由於複製時間長,調用到後臺複製。然後再調用到前臺。期間再暫停,調用到後臺,最後kill掉此進程

[root@ freeit ~]# cp -r /usr /tmp/ &

[1] 3991

//開始複製,&爲後臺執行

 

[root@ freeit ~]# jobs

[1]+   Running                 cp -i -r  /usr /tmp/ &

//查看後臺執行的操作

 

[root@ freeit ~]# fg 1

cp -i -r /usr /tmp/

//調用到前臺執行進程

^Z

[1]+   Stopped                 cp -i -r  /usr /tmp

//ctrl+z停止進程

 

[root@ freeit ~]# bg 1

[1]+ cp -i -r /usr /tmp/ &

//繼續調用到後臺執行

 

[root@ freeit ~]# jobs

[1]+   Running                 cp -i -r  /usr /tmp/ &

 

 [root@ freeit ~]# kill %1

//結束此進程

 

[root@ freeit ~]# jobs

[1]+   Terminated              cp -i -r  /usr /tmp/

 



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