service命令
service命令其实是去/etc/init.d目录下,去执行相关程序
1 # service命令启动redis脚本
2 service redis start
3 # 直接启动redis脚本
4 /etc/init.d/redis start
5 # 开机自启动
6 update-rc.d redis defaults
systemctl命令
systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。
开机第一个程序从init完全换成了systemd这种启动方式。systemd是靠管理unit的方式来控制开机服务,开机级别等功能。
在/usr/lib/systemd/system目录下包含了各种unit文件,有service后缀的服务unit,有target后缀的开机级别unit等,这里介绍关于service后缀的文件。因为systemd在开机要想执行自启动,都是通过这些*.service 的unit控制的,服务又分为系统服务(system)和用户服务(user)。
系统服务:开机不登陆就能运行的程序(常用于开机自启)。
用户服务:需要登陆以后才能运行的程序。
systemd对应的进程管理命令是systemctl。
1 常见 systemctl 命令:
2 [root@localhost ~]# systemctl stop supervisord
3 [root@localhost ~]# systemctl start supervisord
4 [root@localhost ~]# systemctl status supervisord
5 [root@localhost ~]# systemctl restart supervisord
6 [root@localhost ~]# systemctl is-active supervisord # 查看服务是否运行
7 [root@localhost ~]# systemctl enable supervisord # 设置服务允许开机启动
8 [root@localhost ~]# systemctl disable supervisord # 设置服务禁止开机启动
9 [root@localhost ~]# systemctl is-enabled supervisord # 查看服务是否设置为开机启动,得到的值可以是enable、disable或static,这里的 static 它是指对应的 Unit 文件中没有定义[Install]区域,因此无法配置为开机启动服务。
10 [root@localhost ~]# systemctl daemon-reload # 添加.service文件后,或者变更.service文件设置后,需要执行一下这个
11 [root@localhost ~]# systemctl list-unit-files # 查看开机启动项
12 [root@localhost ~]# systemctl list-unit-files | grep 程序名称 # 查看某些服务开机启动状态
13 [root@localhost ~]# systemctl list-unit-files | grep enable # 查看哪些为开机启动服务
14 [root@localhost ~]# systemctl list-units --type=service # 查看所有已启动的服务
15 [root@localhost ~]# systemctl --failed # 查看启动失败的服务列表
16 [root@localhost ~]# systemctl mask supervisord # 注销supervisord服务
17 [root@localhost ~]# systemctl unmask supervisord # 取消注销supervisord服务
18 [root@localhost ~]# systemctl get-default # 获得当前的运行级别
19 [root@localhost ~]# systemctl set-default multi-user.target # 设置默认的运行级别为mulit-user,即开机不开启图形
20 [root@localhost ~]# systemctl set-default graphical.target # 设置默认的运行级别为mulit-user,即开机启动图形
21 [root@localhost ~]# systemctl isolate multi-user.target # 在不重启的情况下,切换到运行级别mulit-user下
22 [root@localhost ~]# systemctl isolate graphical.target # 在不重启的情况下,切换到图形界面下
1)systemctl命令兼容了service
即systemctl也会去/etc/init.d目录下,查看,执行相关程序
1 systemctl redis start
2 systemctl redis stop
3 # 开机自启动
4 systemctl enable redis
2)systemctl命令管理systemd的资源Unit
systemd的Unit放在目录/usr/lib/systemd/system(Centos)或/etc/systemd/system(Ubuntu)
serivces配置文件说明
[Unit] 区块:启动顺序与依赖关系
Description字段:给出当前服务的简单描述。
Documentation字段:给出文档位置。
After字段:如果network.target或sshd-keygen.service需要启动,那么sshd.service应该在它们之后启动。
Before字段:定义sshd.service应该在哪些服务之前启动。
注:After和Before字段只涉及启动顺序,不涉及依赖关系。
1 举例来说,某 Web 应用需要 postgresql 数据库储存数据。在配置文件中,它只定义要在 postgresql 之后启动,而没有定义依赖 postgresql 。上线后,由于某种原因,postgresql 需要重新启动,在停止服务期间,该 Web 应用就会无法建立数据库连接。
2 设置依赖关系,需要使用Wants字段和Requires字段。
3 Wants字段:表示sshd.service与sshd-keygen.service之间存在"弱依赖"关系,即如果"sshd-keygen.service"启动失败或停止运行,不影响sshd.service继续执行。
4 Requires字段则表示"强依赖"关系,即如果该服务启动失败或异常退出,那么sshd.service也必须退出。
5 注意,Wants字段与Requires字段只涉及依赖关系,与启动顺序无关,默认情况下是同时启动的。
[Service] 区块:启动行为
① 启动命令
ExecStart字段:定义启动进程时执行的命令
ExecReload字段:重启服务时执行的命令
ExecStop字段:停止服务时执行的命令
ExecStartPre字段:启动服务之前执行的命令
ExecStartPost字段:启动服务之后执行的命令
ExecStopPost字段:停止服务之后执行的命令
注:所有的启动设置之前,都可以加上一个连词号(-),表示"抑制错误",即发生错误的时候,不影响其他命令的执行。比如EnvironmentFile=-/etc/sysconfig/sshd(注意等号后面的那个连词号),就表示即使/etc/sysconfig/sshd文件不存在,也不会抛出错误。
注意:[Service]中的启动、重启、停止命令全部要求使用绝对路径!
② 启动类型
Type字段定义启动类型。它可以设置的值如下:
simple(默认值):ExecStart字段启动的进程为主进程
forking:ExecStart字段将以fork()方式启动,此时父进程将会退出,子进程将成为主进程(后台运行)
oneshot:类似于simple,但只执行一次,Systemd 会等它执行完,才启动其他服务
dbus:类似于simple,但会等待 D-Bus 信号后启动
notify:类似于simple,启动结束后会发出通知信号,然后 Systemd 再启动其他服务
idle:类似于simple,但是要等到其他任务都执行完,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混合
③ 重启行为
Service区块有一些字段,定义了重启行为:
KillMode字段:定义 Systemd 如何停止 sshd 服务:
control-group(默认值):当前控制组里面的所有子进程,都会被杀掉
process:只杀主进程
mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号
none:没有进程会被杀掉,只是执行服务的 stop 命令。
Restart字段:定义了 sshd 退出后,Systemd 的重启方式
上面的例子中,Restart设为on-failure,表示任何意外的失败,就将重启sshd。如果 sshd 正常停止(比如执行systemctl stop命令),它就不会重启。
Restart字段可以设置的值如下:
no(默认值):退出后不会重启
on-success:只有正常退出时(退出状态码为0),才会重启
on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启
on-abnormal:只有被信号终止和超时,才会重启
on-abort:只有在收到没有捕捉到的信号终止时,才会重启
on-watchdog:超时退出,才会重启
always:不管是什么退出原因,总是重启
注:对于守护进程,推荐设为on-failure。对于那些允许发生错误退出的服务,可以设为on-abnormal。
RestartSec字段:表示 Systemd 重启服务之前,需要等待的秒数。
上面的例子设为等待42秒。
[Install] 区块
Install区块,定义如何安装这个配置文件,即怎样做到开机启动。
WantedBy字段:表示该服务所在的 Target。
Target的含义是服务组,表示一组服务。
WantedBy=multi-user.target指的是:sshd 所在的 Target 是multi-user.target。
这个设置非常重要,因为执行systemctl enable sshd.service命令时,sshd.service的一个符号链接,就会放在/etc/systemd/system目录下面的multi-user.target.wants子目录之中。
# Systemd 有默认的启动 Target。
1 # systemctl get-default
2 multi-user.target
上面的结果表示,默认的启动 Target 是multi-user.target。在这个组里的所有服务,都将开机启动。这就是为什么systemctl enable命令能设置开机启动的原因。
使用 Target 的时候,systemctl list-dependencies命令和systemctl isolate命令也很有用。
1 # 查看 multi-user.target 包含的所有服务
2 systemctl list-dependencies multi-user.target
3
4 # 切换到另一个 target
5 # shutdown.target 就是关机状态
6 systemctl isolate shutdown.target
一般来说,常用的 Target 有两个:
multi-user.target:表示多用户命令行状态;
graphical.target:表示图形用户状态,它依赖于multi-user.target。
注册服务实例介绍
- 配置文件目录
systemctl脚本目录:/usr/lib/systemd/
系统服务目录:/usr/lib/systemd/system/
用户服务目录:/usr/lib/systemd/user/
- 在/usr/lib/systemd/system目录下新建service-name.service文件:
1 [UNIT]
2 # 服务描述
3 Description=Media wanager Service
4 # 指定了在systemd在执行完那些target之后再启动该服务
5 After=network.target
6
7 [Service]
8 # 定义Service的运行类型,一般是forking(后台运行)
9 Type=forking
10
11 # 定义systemctl start|stop|reload *.service 的执行方法(具体命令需要写绝对路径)
12 # 注:ExecStartPre为启动前执行的命令
13 ExecStartPre=/usr/bin/test "x${NETWORKMANAGER}" = xyes
14 ExecStart=/home/mobileoa/apps/shMediaManager.sh -start
15 ExecReload=
16 ExecStop=/home/mobileoa/apps/shMediaManager.sh -stop
17
18 # 创建私有的内存临时空间
19 PrivateTmp=True
20
21 [Install]
22 # 多用户
23 WantedBy=multi-user.target
重载系统服务:systemctl daemon-reload
设置开机启动:systemctl enable xxx.service
启动服务:systemctl start xxx.service
停止服务:systemctl stop xxx.service
重启服务:systemctl reload xxx.service
修改完配置文件要重载配置文件。
启动分析
① 分析关键耗时
1 # systemd-analyze critical-chain
② 分析每个服务的耗时
1 # systemd-analyze blame
③ 将每个服务的耗时输出到图片
1 # systemd-analyze plot > boot.svg
④ 列出所有开机自动启动的服务
1 # systemctl list-unit-files --type=service | grep enabled
Systemctl和service、chkconfig命令的关系
systemctl命令:是一个systemd工具,主要负责控制systemd系统和服务管理器。
service命令:可以启动、停止、重新启动和关闭系统服务,还可以显示所有系统服务的当前状态。
chkconfig命令:是管理系统服务(service)的命令行工具。所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序。
systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。
systemctl是RHEL 7 的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。可以使用它永久性或只在当前会话中启用/禁用服务。
所以systemctl命令是service命令和chkconfig命令的集合和代替。
例如:使用service启动服务实际上也是调用systemctl命令。
[root@localhost ~]# service httpd start
Redirecting to /bin/systemctl start httpd.service
service文件定义
1 .service文件定义了一个服务分为:[Unit],[Service],[Install]三个小节
2
3 [Unit]
4 Description:描述,
5 After:在network.target,auditd.service启动后才启动
6 ConditionPathExists: 执行条件
7
8 [Service]
9 EnvironmentFile:变量所在文件
10 ExecStart: 执行启动脚本
11 Restart: fail时重启
12
13 [Install]
14 Alias:服务别名
15 WangtedBy: 多用户模式下需要的
Systemctl命令的用法:
Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。
Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。
systemd即为system daemon,是linux下的一种init软件。
(1)列出所有可用单元:
1 [root@localhost ~]# systemctl list-unit-files
2 UNIT FILE STATE
3 proc-sys-fs-binfmt_misc.automount static
4 dev-hugepages.mount static
5 dev-mqueue.mount static
6 proc-fs-nfsd.mount static
7 proc-sys-fs-binfmt_misc.mount static
8 sys-fs-fuse-connections.mount static
9 sys-kernel-config.mount static
10 sys-kernel-debug.mount static
11 tmp.mount disabled
12 var-lib-nfs-rpc_pipefs.mount static
13 brandbot.path disabled
14 cups.path enabled
(2)列出所有可用单元:
1 [root@localhost ~]# systemctl list-units
2 UNIT LOAD ACTIVE SUB DESCRIPTION
3 proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary
4 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda
5 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda
6 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda
7 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb
8 sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb
9 sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loade
10 sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device lo
11 ..............
(3)列出所有失败单元:
1 [root@localhost ~]# systemctl --failed
2 UNIT LOAD ACTIVE SUB DESCRIPTION
3 ● network.service loaded failed failed LSB: Bring up/down networking
4 ● teamd@team0.service loaded failed failed Team Daemon for device team0
5
6 LOAD = Reflects whether the unit definition was properly loaded.
7 ACTIVE = The high-level unit activation state, i.e. generalization of SUB
8 SUB = The low-level unit activation state, values depend on unit type.
9
10 2 loaded units listed. Pass --all to see loaded but inactive units, too.
11 To show all installed unit files use 'systemctl list-unit-files'.
(4)检查某个单元是否启动:
1 [root@localhost ~]# systemctl is-enabled httpd.service
2 enabled
(5)检查某个服务的运行状态:
1 [root@localhost ~]# systemctl status httpd.service
2 ● httpd.service - The Apache HTTP Server
3 Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
4 Active: active (running) since 日 2018-10-14 18:21:46 CST; 1 day 2h ago
5 Docs: man:httpd(8)
6 man:apachectl(8)
7 Main PID: 19020 (httpd)
8 Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
9 CGroup: /system.slice/httpd.service
10 ├─19020 /usr/sbin/httpd -DFOREGROUND
11 ├─27310 /usr/sbin/httpd -DFOREGROUND
12 ├─27311 /usr/sbin/httpd -DFOREGROUND
13 ├─27312 /usr/sbin/httpd -DFOREGROUND
14 ├─27313 /usr/sbin/httpd -DFOREGROUND
15 └─27314 /usr/sbin/httpd -DFOREGROUND
16
17 10月 14 18:21:46 localhost systemd[1]: Starting The Apache HTTP Serv....
18 10月 14 18:21:46 localhost httpd[19020]: AH00558: httpd: Could not r...e
(6)列出所有服务:
1 [root@localhost ~]# systemctl list-unit-files --type=service
2 UNIT FILE STATE
3 abrt-ccpp.service enabled
4 abrt-oops.service enabled
5 abrt-pstoreoops.service disabled
6 abrt-xorg.service enabled
7 abrtd.service enabled
8 accounts-daemon.service enabled
9 alsa-restore.service static
10 alsa-state.service static
11 alsa-store.service static
12 arp-ethers.service disabled
13 atd.service disabled
14 auditd.service enabled
15 auth-rpcgss-module.service static
(7)启动,停止,重启服务等:
1 [root@localhost ~]# systemctl restart httpd.service
2 # systemctl restart httpd.service
3 # systemctl stop httpd.service
4 # systemctl reload httpd.service
5 # systemctl status httpd.service
(8)查询服务是否激活,和配置是否开机启动:
1 [root@localhost ~]# systemctl is-active httpd
2 active
3 [root@localhost ~]# systemctl disable httpd
4 Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
5 [root@localhost ~]# systemctl enable httpd
6 Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
(9)使用systemctl命令杀死服务:
1 [root@localhost ~]# systemctl kill httpd
(10)列出系统的各项服务,挂载,设备等:
1 [root@localhost ~]# systemctl list-unit-files --type
2 automount device path snapshot swap timer
3 busname mount service socket target
(11)获得系统默认启动级别和设置默认启动级别:
1 [root@localhost ~]# systemctl get-default
2 graphical.target
3 [root@localhost ~]# systemctl set-default multi-user.target
(12)启动运行等级:
1 systemctl isolate multiuser.target
(13)重启、停止,挂起、休眠系统等:
1 # systemctl reboot
2 # systemctl halt
3 # systemctl suspend
4 # systemctl hibernate
5 # systemctl hybrid-sleep
chkconfig命令用法:
chkconfig是管理系统服务(service)的命令行工具。所谓系统服务(service),就是随系统启动而启动,随系统关闭而关闭的程序。
chkconfig可以更新(启动或停止)和查询系统服务(service)运行级信息。更简单一点,chkconfig是一个用于维护/etc/rc[0-6].d目录的命令行工具。
1 [root@localhost ~]# chkconfig --help
2 chkconfig 版本 1.7.2 - 版权 (C) 1997-2000 红帽公司
3 在 GNU 公共许可条款下,本软件可以自由重发行。
4
5 用法: chkconfig [--list] [--type <类型>] [名称]
6 chkconfig --add <名称>
7 chkconfig --del <名称>
8 chkconfig --override <名称>
9 chkconfig [--level <级别>] [--type <类型>] <名称> <on|off|reset|resetpriorities>
(一)设置service开机是否启动:
1 chkconfig name on/off/reset
on、off、reset用于改变service的启动信息。
on表示开启,off表示关闭,reset表示重置。
默认情况下,on和off开关只对运行级2,3,4,5有效,reset可以对所有运行级有效。
1 [root@localhost ~]# chkconfig httpd on
2 注意:正在将请求转发到“systemctl enable httpd.service”
在Redhat7上,运行chkconfig命令,都会被转到systemcle命令上。
(2)设置service运行级别:
1 chkconfig --level levels
该命令可以用来指定服务的运行级别,即指定运行级别2,3,4,5等。
等级0表示:表示关机
等级1表示:单用户模式
等级2表示:无网络连接的多用户命令行模式
等级3表示:有网络连接的多用户命令行模式
等级4表示:不可用
等级5表示:带图形界面的多用户模式
等级6表示:重新启动
例如:
1 [root@localhost ~]# chkconfig --level 5 httpd on
2 注意:正在将请求转发到“systemctl enable httpd.service”
(三)列出service启动信息:
1 # chkconfig --list [name]
如果不指定name,会列出所有services的信息。
每个service每个运行级别都会有一个启动和停止脚本;当切换运行级别时,init不会重启已经启动的service,也不会重新停止已经停止的service。
例如:
1 [root@localhost ~]# chkconfig --list
2
3 注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
4 如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
5 欲查看对特定 target 启用的服务请执行
6 'systemctl list-dependencies [target]'。
7
8 netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
9 network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
10 rhnsd 0:关 1:关 2:开 3:开 4:开 5:开 6:关
11 **总结:**service命令的功能基本都被systemct取代。直接使用systemctl命令即可。