Linux系统服务器停服,Linux系统服务启动和停止

systemd 是在Linux上运行服务的新方式。 systemd 有一个被替代的 sysvinit 。 systemd

为Linux带来更快的启动时间,现在是管理Linux服务的标准方式。虽然稳定, systemd 仍在不断发展。

systemd 作为init系统,用于管理Linux内核引导后需要更改状态的服务和守护进程。通过状态改变开始,停止,重新加载和调整服务状态被应用。

首先,我们来检查当前在我们的服务器上运行的systemd的版本。

[centos@localhost ~]$ systemctl --version

systemd 219

+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP

+GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN

[centos@localhost ~]$

从CentOS版本7开始,在编写本系统时219完全更新了当前的稳定版本。

我们还可以使用 systemd-analyze 分析上次服务器启动时间 __

[centos@localhost ~]$ systemd-analyze

Startup finished in 1.580s (kernel) + 908ms (initrd) + 53.225s (userspace) = 55.713s

[centos@localhost ~]$

当系统启动时间较慢时,我们可以使用 systemd-analyze blame 命令。

[centos@localhost ~]$ systemd-analyze blame

40.882s kdump.service

5.775s NetworkManager-wait-online.service

4.701s plymouth-quit-wait.service

3.586s postfix.service

3.121s systemd-udev-settle.service

2.649s tuned.service

1.848s libvirtd.service

1.437s network.service

875ms packagekit.service

855ms gdm.service

514ms firewalld.service

438ms rsyslog.service

436ms udisks2.service

398ms sshd.service

360ms boot.mount

336ms polkit.service

321ms accounts-daemon.service

在使用 systemd时 ,理解 单元 的概念很重要。 单位 是 系统 知道如何解释的资源。单位分为以下12种类型 -

.service

.socket

.device

.mount

.automount

.swap

.target

.path

.timer

.snapshot

.slice

.scope

大多数情况下,我们将以.service作为单位目标。建议对其他类型进一步研究。由于只有 服务 单位将适用于启动和停止 系统 服务。

每个 单位 都是在一个文件中定义的 -

/lib/systemd/system - 基本单元文件

/etc/systemd/system - 修改后的单元文件在运行时启动

使用systemctl管理服务

要使用 systemd ,我们需要非常熟悉 systemctl 命令。以下是 systemctl 最常用的命令行开关。

参数

动作

-t

单位类型的逗号分隔值,如服务或套接字

-a

显示所有加载的单位

--state

显示处于已定义状态的所有设备:负载,子设备,活动设备,非活动设备等。

-H

远程执行操作。指定由@分隔的主机名或主机和用户。

基本的systemctl用法

systemctl [operation]

example: systemctl --state [servicename.service]

快速查看我们的盒子上运行的所有服务。

[root@localhost rdc]# systemctl -t service

UNIT LOAD ACTIVE SUB DESCRIPTION

abrt-ccpp.service loaded active exited Install ABRT coredump hook

abrt-oops.service loaded active running ABRT kernel log watcher

abrt-xorg.service loaded active running ABRT Xorg log watcher

abrtd.service loaded active running ABRT Automated Bug Reporting Tool

accounts-daemon.service loaded active running Accounts Service

alsa-state.service loaded active running Manage Sound Card State (restore and store)

atd.service loaded active running Job spooling tools

auditd.service loaded active running Security Auditing Service

avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack

blk-availability.service loaded active exited Availability of block devices

bluetooth.service loaded active running Bluetooth service

chronyd.service loaded active running NTP client/server

停止服务

首先,停止蓝牙服务。

[root@localhost]# systemctl stop bluetooth

[root@localhost]# systemctl --all -t service | grep bluetooth

bluetooth.service loaded inactive dead Bluetooth service

[root@localhost]#

正如我们所看到的,蓝牙服务现在处于非活动状态。

再次启动蓝牙服务。

[root@localhost]# systemctl start bluetooth

[root@localhost]# systemctl --all -t service | grep bluetooth

bluetooth.service loaded active running Bluetooth service

[root@localhost]#

注意 - 我们没有指定bluetooth.service,因为 隐含了 .service 。考虑附加我们正在处理的服务的 单元类型

是一个好习惯。所以,从这里开始,我们将使用 .service 扩展来阐明我们正在开展的服务单元操作。

可以在服务上执行的主要操作是 -

Start

开始服务

Stop

停止服务

Reload

重新加载没有停止它的服务的活动配置(如在系统v init中kill -HUP)

Restart

启动,然后停止服务

Enable

在启动时启动服务

Disable

阻止服务在运行时自动启动

以上操作主要用于以下场景

Start

将服务置于停止状态。.

Stop

要暂时关闭服务(例如,必须停止服务才能访问服务锁定的文件,就像升级服务时一样)

Reload

在编辑配置文件时,我们希望在不停止服务的同时应用新更改。

Restart

在与重新加载相同的情况下,但该服务不支持重新加载。

Enable

当我们希望禁用的服务在启动时运行。

Disable

主要用于有需要停止服务时,但在启动时启动。

检查服务的状态

[root@localhost]# systemctl status network.service

network.service - LSB: Bring up/down networking

Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)

Active: active (exited) since Sat 2017-01-14 04:43:48 EST; 1min 31s ago

Docs: man:systemd-sysv-generator(8)

Process: 923 ExecStart = /etc/rc.d/init.d/network start (code=exited, status = 0/SUCCESS)

localhost.localdomain systemd[1]: Starting LSB: Bring up/down networking...

localhost.localdomain network[923]: Bringing up loopback interface: [ OK ]

localhost.localdomain systemd[1]: Started LSB: Bring up/down networking.

[root@localhost]#

向我们展示 网络 服务的当前状态。如果我们想看到所有与网络相关的服务,我们可以使用 -

[root@localhost]# systemctl --all -t service | grep -i network

network.service loaded active exited LSB: Bring up/

NetworkManager-wait-online.service loaded active exited Network Manager

NetworkManager.service loaded active running Network Manager

ntpd.service loaded inactive dead Network Time

rhel-import-state.service loaded active exited Import network

[root@localhost]#

对于那些熟悉管理服务的 sysinit 方法的人来说,转换到 systemd 是很重要的。 systemd

是在Linux中启动和停止守护程序服务的新方式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值