码迷,mamicode.com
首页 > 其他好文 > 详细

service与systemctl命令比较

时间:2020-07-11 10:06:04      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:子进程   arp   reject   nbsp   centos 7   关闭   systemctl   loop   原来   

本文将比较 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命令比较

标签:子进程   arp   reject   nbsp   centos 7   关闭   systemctl   loop   原来   

原文地址:https://www.cnblogs.com/chenbin93/p/13282165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!