本文将比较 linux 的 service 和 systemctl 命令,先分别简单介绍这两个命令的基础用法,然后进行比较。

从 CentOS 7.x 开始,CentOS 开始使用 systemd 服务来代替 daemon,原来管理系统启动和管理系统服务的相关命令全部由 systemctl命 令来代替。

service 命令

service命令是Redhat Linux兼容的发行版中用来控制系统服务的实用工具,它以启动、停止、重新启动和关闭系统服务,还可以显示所有系统服务的当前状态。

语法: service < option > | --status-all | [ service_name [ command | --full-restart ] ]

option 的值

  • -h:显示 service 的帮助信息

  • -status:显示所服务的状态

  • --status-all:查看所有服务的状态

  • service_name:服务名,即 /etc/init.d 目录下的脚本文件名

  • command:系统服务脚本支持的控制命令,如:start、stop 和 restart

  • --full-restart:重启所有服务

实例:查看 service 的帮助信息

1
2
3
[root@localhost ~]# service -h
Usage: service < option > | --status-all | [ service_name [ command | --full-restart ] ]
[root@localhost ~]#

实例2:查看所有的服务状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@centos-x64 ~]# service --status-all
auditd (pid  1299) is running...
Stopped
cgred is stopped
crond (pid  1481) is running...
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     all      ::/0                 ::/0                state RELATED,ESTABLISHED
2    ACCEPT     icmpv6    ::/0                 ::/0
3    ACCEPT     all      ::/0                 ::/0
4    ACCEPT     tcp      ::/0                 ::/0                state NEW tcp dpt:22
5    ACCEPT     tcp      ::/0                 ::/0                state NEW tcp dpt:80
6    REJECT     all      ::/0                 ::/0                reject-with icmp6-adm-prohibited
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination
1    REJECT     all      ::/0                 ::/0                reject-with icmp6-adm-prohibited

实例3: 使用 service 启动/重启/停止网络服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@centos-x64 ~]# service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
 
[root@centos-x64 ~]# service network start
Bringing up loopback interface:               [ OK ]
Bringing up interface eth0:                   [ OK ]
 
[root@centos-x64 ~]# service network stop
Bringing dwon interface eth0:                   [ OK ]
Bringing down loopback interface:               [ OK ]
 
[root@centos-x64 ~]# service network status
Configured devices:
lo eth0
Currently active devices:
lo eth0

systemctl 命令

历史上,Linux 的启动一直采用init进程。下面的命令用来启动服务。

1
2
3
sudo /etc/init.d/apache2 start
# 或者
$ service apache2 start

这种方法有两个缺点:

  • 一是启动时间长。init 进程是串行启动,只有前一个进程启动完,才会启动下一个进程。

  • 二是启动脚本复杂。init 进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长

Systemd 就是为了解决上面问题而诞生的。它的设计目标是,为系统的启动和管理提供一套完整的解决方案。根据 Linux 惯例,字母 d 是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。使用了 Systemd,就不需要再用 init 了。Systemd 取代了 initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。

1
2
3
4
[root@localhost ~]# systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
[root@localhost ~]#

Systemd 的优点是功能强大,使用方便,缺点是体系庞大,非常复杂。事实上,现在还有很多人反对使用 Systemd,理由就是它过于复杂,与操作系统的其他部分强耦合,违反 “keep simple, keep stupid” 的Unix 哲学。

实例1:systemctl常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 重启系统
sudo systemctl reboot
# 关闭系统,切断电源
sudo systemctl poweroff
# CPU停止工作
sudo systemctl halt
# 暂停系统
sudo systemctl suspend
# 让系统进入冬眠状态
sudo systemctl hibernate
# 让系统进入交互式休眠状态
sudo systemctl hybrid-sleep
# 启动进入救援状态(单用户状态)
sudo systemctl rescue

实例2:检查systemd和systemctl的二进制文件和库的安装位置。

1
2
3
4
# whereis systemd 
systemd: /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
# whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

实例3:检查systemd是否正在运行

1
2
3
4
5
6
# ps -eaf | grep [s]ystemd
root         1     0  0 16:27 ?        00:00:00 /usr/lib/systemd/systemd --switched-root --system --deserialize 23
root       444     1  0 16:27 ?        00:00:00 /usr/lib/systemd/systemd-journald
root       469     1  0 16:27 ?        00:00:00 /usr/lib/systemd/systemd-udevd
root       555     1  0 16:27 ?        00:00:00 /usr/lib/systemd/systemd-logind
dbus       556     1  0 16:27 ?        00:00:00 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation

实例4:列出所有服务(包括启用和禁用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# systemctl list-unit-files --type=service
UNIT FILE                                   STATE
arp-ethers.service                          disabled
auditd.service                              enabled
autovt@.service                             disabled
blk-availability.service                    disabled
brandbot.service                            static
collectd.service                            disabled
console-getty.service                       disabled
console-shell.service                       disabled
cpupower.service                            disabled
crond.service                               enabled
dbus-org.fedoraproject.FirewallD1.service   enabled
....

service 与 systemctl 命令对比

daemon命令 systemctl命令 说明
service [服务] start systemctl start [unit type] 启动服务
service [服务] stop systemctl stop [unit type] 停止服务
service [服务] restart systemctl restart [unit type] 重启服务

service与systemctl命令比较的更多相关文章

  1. Centos7下的systemctl命令与service和chkconfig

    博主使用的操作系统是最新的CentOS 7,所以可能和网上一些老的博文有一定出入,那是因为版本更新的原因. 这里写图片描述1 service service命令用于对系统服务进行管理,比如启动(sta ...

  2. 更新换代----systemctl命令取代chkconfig和service

    systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 ...

  3. Systemctl和service、chkconfig命令的关系

      systemctl命令:是一个systemd工具,主要负责控制systemd系统和服务管理器. service命令:可以启动.停止.重新启动和关闭系统服务,还可以显示所有系统服务的当前状态. ch ...

  4. Centos7中systemctl命令详解

    Linux Systemctl是一个系统管理守护进程.工具和库的集合,用于取代System V.service和chkconfig命令,初始进程主要负责控制systemd系统和服务管理器.通过Syst ...

  5. 【搬运】systemctl 命令完全指南

    Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器. Systemd是一个系统管理守护进程.工具和库的集合,用于取代System V初始进程.Systemd的功能是 ...

  6. systemctl命令用法详解

    systemctl命令用法详解系统环境:Fedora 16binpath:/bin/systemctlpackage:systemd-units systemctl enable httpd.serv ...

  7. centos7 systemctl命令

    systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 实例: 启动nfs服务:systemctl start nfs-server.s ...

  8. systemctl命令

    systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起. 任务 旧指令 新指令 使某服务自动启动 chkconfig --level 3 ...

  9. centos7中systemctl命令使用方法和心得体会

    使用linux的同学对service和chkconfig两个命令都不陌生,其重要性不言而喻,那么怎么会突然冒出个systemctl命令呢?其实,为了简化操作,systemctl命令将service和c ...

随机推荐

  1. spring-data-jpa -hibernate --specificationExecutor

    Specifications动态查询 在查询某个实体的时候,给定的条件是不固定的,这时就需要动态构建相应的查询语句,在Spring Data JPA中可以通过JpaSpecificationExecu ...

  2. 贝塞尔曲线(面)二三维可视化(Three+d3)

    贝塞尔曲线(面)二三维可视化(Three+d3) 在学完 games101 几何后开始实践,可视化贝塞尔曲线 我想实现三维的贝塞尔曲线,用 threejs,但是 threejs 控制太麻烦了,因此,我 ...

  3. 白话TCP/IP原理

    TCP/IP(Transmission-Control-Protocol/Internet-Protocol),中文译名为传输控制协议/因特网互联协议,又名网络通讯协议,是Internet最基本的协议 ...

  4. 【刷题-LeetCode】304. Range Sum Query 2D - Immutable

    Range Sum Query 2D - Immutable Given a 2D matrix matrix, find the sum of the elements inside the rec ...

  5. 【小问题】为啥乱搞就不行,golang没安装在系统目录下,导致go get出现"package bytes: directory "/home/ahfu/go/src/bytes" is not using a known version control system"

    想在自己的账号下安装golang开发环境,于是这样配置: wget https://dl.google.com/go/go1.14.2.linux-amd64.tar.gz cd /home/ahfu ...

  6. 适配器模式(Adapter模式)

    模式的定义与特点 适配器模式(Adapter)的定义如下:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作.适配器模式分为类结构型模式和对象结构型模式 ...

  7. django之memcached缓存系统

    django其他缓存方法:(https://www.cnblogs.com/jishuweiwang/p/6110809.html) memcached版本 <1.5 1. memcached缓 ...

  8. from 表单上传多个文件?

    和单个上传文件基本相同,就是需要在后台控制器中,用数组来接收 jsp页面提交过来的file数据. 也分为三个部分演示. 一.jsp <%-- Created by IntelliJ IDEA. ...

  9. 【ASP.NET Core】设置Web API 响应的数据格式——Produces 特性篇

    开春首文,今天老周就跟各位大伙伴们聊一个很简单的话题:怎么设定API响应的数据格式. 说本质一点,就是设置所返回内容的 MIME 类型(Content-Type 头).当然了,咱们不会使用在HTTP管 ...

  10. ApacheCN PHP 译文集 20211101 更新

    PHP 入门指南 零.序言 一.PHP 入门 二.数组和循环 三.函数和类 四.数据操作 五.构建 PHP Web 应用 六.搭建 PHP 框架 七.认证与用户管理 八.建立联系人管理系统 使用 PH ...