第五章:系统服务-创新互联

第五章:系统服务

成都创新互联公司长期为上千多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为天祝藏族自治企业提供专业的成都网站制作、网站建设、外贸网站建设天祝藏族自治网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。

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 /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 id:id空闲CPU百分比;

  • 0.0 wa:wa等待输入输出的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:进程优先级;

  • NI:nice值。负值表示高优先级;正值表示低优先级;

  • VIRT:进程使用的虚拟内存总量,单位Kb,VITR=SWAP+RES

  • RES:进程使用的、未被交换出的物理内存大小,单位KB,RES=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/

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站栏目:第五章:系统服务-创新互联
网址分享:http://myzitong.com/article/dcocdc.html