前言
Linux内核加载启动后,用户空间的第一个进程就是初始化进程,这个程序的物理文件约定位于/sbin/init,当然也可以通过传递内核参数来让内核启动指定的程序。这个进程的特点是进程号为1,代表第一个运行的用户空间进程。不同发行版采用了不同的启动程序,主要有以下几种主流选择:
(1)以Ubuntu为代表的Linux发行版采用upstart。
(2)以7.0版本之前的CentOS为代表的System V init。
(3)CentOS7.0版本的systemd。
systemctl命令:是一个systemd工具,主要负责控制systemd系统和服务管理器。
service命令:可以启动、停止、重新启动和关闭系统服务,还可以显示所有系统服务的当前状态。
chkconfig命令:是管理系统服务(service)的命令行工具。所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序。
systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。
例如:使用service启动服务实际上也是调用systemctl命令。
[root@localhost ~]# service httpd start
Redirecting to /bin/systemctl start httpd.service
一、service
service命令其实是去/etc/init.d目录下
语法: 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:重启所有服务
历史上,Linux 的启动一直采用init进程。下面的命令用来启动服务。
1 $ sudo /etc/init.d/apache2 start
2 # 或者
3 $ service apache2 start
这种方法有两个缺点:
• 一是启动时间长。init 进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
• 二是启动脚本复杂。init 进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长
Systemd 就是为了解决上面问题而诞生的。根据 Linux 惯例,字母 d 是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。使用了 Systemd,就不需要再用 init 了。Systemd 取代了 initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。
二、systemd/systemctl
Systemd 就是为了解决上面问题而诞生的。根据 Linux 惯例,字母 d 是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。使用了 Systemd,就不需要再用 init 了。Systemd 取代了 initd,成为系统的第一个进程(PID 等于 1),其他进程都是它的子进程。
systemctl --version
上面的命令查看 Systemd 的版本。
systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。
systemd对应的进程管理命令是systemctl
1)systemctl命令兼容了service
即systemctl也会去/etc/init.d目录下,查看,执行相关程序
(上图为 Systemd 架构图)
具体可以查看http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
Systemd 并不是一个命令,而是一组命令,涉及到系统管理的方方面面。
systemctl是 Systemd 的主命令,用于管理系统。
个人猜测
systemV Init --> service
Systemd -->systemctl
systemctl常用命令
注意:如systemctl start httpd是systemctl start httpd.service的简写
启动服务:
systemctl start httpd.service
停掉服务:
systemctl stop httpd.service
systemctl restart httpd.service
systemctl try-restart httpd.service
systemctl reload httpd.service
restart选项,如果服务在运行中,它将重启服务;如果服务不在运行中,它将会启动。
try-start选项,它只会在服务已经在运行中的时候重启服务。
reload选项你也可以有,它会重新加载配置文件。
查看服务状态:
systemctl status httpd.service
使用enable/disable选项来控制一个服务是否开机启动,命令如下:
systemctl enable httpd.service
systemctl disable httpd.service
查看服务是否开机启动:
systemctl is-enabled postfix.service
查看已启动的服务列表
systemctl list-unit-files | grep enabled
以树形列出正在运行的进程,它可以递归显示控制组内容
systemd-cgls
查询服务是否激活:
systemctl is-active httpd
使用systemctl命令杀死服务:
systemctl kill httpd
列出所有正在运行的服务
systemctl
列出激活的单元
systemctl list-units
列出运行失败的单元
systemctl --failed
列出所有已安装服务
systemctl list-unit-files
三、chkconfig
chkconfig命令用法
chkconfig是管理系统服务(service)的命令行工具。所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序。
chkconfig可以更新(启动或停止)和查询系统服务(service)运行级信息。更简单一点,chkconfig是一个用于维护/etc/rc[0-6].d目录的命令行工具。
chkconfig常用命令:
[root@localhost ~]# chkconfig --help
chkconfig 版本 1.7.2 - 版权 (C) 1997-2000 红帽公司
在 GNU 公共许可条款下,本软件可以自由重发行。
用法: chkconfig [--list] [--type <类型>] [名称]
chkconfig --add <名称>
chkconfig --del <名称>
chkconfig --override <名称>
chkconfig [--level <级别>] [--type <类型>] <名称> <on|off|reset|resetpriorities>
(一)设置service开机是否启动:
[root@localhost ~]# chkconfig name on/off/reset
on、off、reset用于改变service的启动信息。
on表示开启,off表示关闭,reset表示重置。
默认情况下,on和off开关只对运行级2,3,4,5有效,reset可以对所有运行级有效。
[root@localhost ~]# chkconfig httpd on
注意:正在将请求转发到“systemctl enable httpd.service”。
在Redhat7上,运行chkconfig命令,都会被转到systemcle命令上。
(2)设置service运行级别:
chkconfig --level levels
该命令可以用来指定服务的运行级别,即指定运行级别2,3,4,5等。
等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无网络连接的多用户命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动
例如:
[root@localhost ~]# chkconfig --level 5 httpd on
注意:正在将请求转发到“systemctl enable httpd.service”
(三)列出service启动信息:
chkconfig --list [name]
如果不指定name,会列出所有services的信息。
每个service每个运行级别都会有一个启动和停止脚本;当切换运行级别时,init不会重启已经启动的service,也不会重新停止已经停止的service。
例如:
[root@localhost ~]# chkconfig --list
注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
欲查看对特定 target 启用的服务请执行
'systemctl list-dependencies [target]'。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
rhnsd 0:关 1:关 2:开 3:开 4:开 5:开 6:关
总结
service命令的功能基本都被systemct取代。直接使用systemctl命令即可
注意:service是service+service+option,而systemctl 是systemctl+option+service