本文将比较 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. 微软开源Kubernetes服务网格项目Open Service Mesh​

    尽管微服务环境提供可移植性,允许更快更频繁的部署周期,甚至还能让组织创建关注于特定领域的团队,但这也伴随着对于流量管理.安全以及可观测性等需求的增长.在整个生态系统中,针对这些需求的服务网格模式的实现 ...

  2. rootckeck

    rootcheck rootcheck1.问题描述2.analysis3.solution4.总结 1.问题描述 经常会有听说root手机,其实质就是让使用手机的人获得手机系统的最大权限.因为andr ...

  3. django框架--登录注册功能(ajax)

    注册 实现一个注册功能 编写 html 内容 input 标签 csrf_token ajax 路由 视图: 提供页面 负责处理业务,返回响应 接收到   post   请求传递的参数 写库 返回   ...

  4. NPOI处理Excel

    using NPOI; using NPOI.XSSF.UserModel; using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; NPOI.SS.U ...

  5. 【刷题-LeetCode】122 Best Time to Buy and Sell Stock II

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  6. 不难懂-------git版本回退

    1. git log 定义:该命令显示从最近到最远的提交日志.每一次提交都有对应的 commit id 和 commit message.   如果嫌弃输出的信息杂乱无章,那么加上 --pretty= ...

  7. Java 将PDF转为HTML时保存到流

    本文介绍如何通过Java后端程序代码将PDF文件转为HTML,并将转换后的HTML文件保存到流.在实现转换时,可设置相关转换属性,如:是否嵌入SVG.是否嵌入图片等.下面是实现转换的方法和步骤: 1. ...

  8. Android开发----使用 Room 将数据保存到本地数据库

    Room介绍以及不使用SQLite的原因 Room 在 SQLite 上提供了一个抽象层,以便在充分利用 SQLite 的强大功能的同时,能够流畅地访问数据库. 处理大量结构化数据的应用可极大地受益于 ...

  9. Mongodb全备+增备+oplog恢复误删数据

    此时测试表中有7条数据,做个全备. 全备: mongodump --host=192.168.43.43 --port=37017 --oplog --out=/opt/mongo/fullbacku ...

  10. 在海外上传文件到中国AWS S3

    s3cmd --access_key= --secret_key=xxxx --region=cn-north-1 --host=s3.cn-north-1.amazonaws.com.cn --ho ...