系统服务chkconfig,systemd

来源:互联网 发布:手机房屋平面设计软件 编辑:程序博客网 时间:2024/05/18 23:56

10.24- 10.25 chkconfig和systemd

chkconfig

windows系统有开机启动项,linux也同样有开机启动项。在centos6上的开机启动项管理工具为chkconfig,所有的开机启动服务都可以在/etc/init.d/目录看到。但是centos7已经不在用chkconfig管理开机启动项。

[root@cent01 sbin]# ls /etc/init.d/functions  mysqld  netconsole  network  README

系统的预设服务都可以通过这种方式启动:
service 服务名 start|restart|stop 服务名就是/etc/init.d/目录下的这些文件。

启动crond服务除了用:service crond start 之外,还可以用 /etc/init.d/crond start

我们可以用chkconfig –list查看所有的服务及其在每个级别的开启状态。

[root@cent01 sbin]# chkconfig --list注:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。       要列出 systemd 服务,请执行 'systemctl list-unit-files'。      查看在具体 target 启用的服务请执行      'systemctl list-dependencies [target]'。mysqld          0:1:2:3:4:5:6:关netconsole      0:1:2:3:4:5:6:

但是这里只显示了SysV管理的服务,centos6及之前都是这个管理工具,但是在centos7用的是systemd管理,所以systemd管理的服务在这里没有显示出来。

运行级别为系统启动级别,具体含义如下:
0 shutdown关机
1 单用户模式
2 无NFS支持的多用户模式
3 完全多用户模式,常用的命令行模式
4 保留给用户自定义
5 图形界面登录,比3多了一个图形界面
6 重启

运行级别0,1,6作为保留模式。通常做设置时,如果不指定级别,则默认为更改2,3,4,5。

[root@cent01 sbin]# chkconfig --level 3 network off  //关闭3级别的network服务[root@cent01 sbin]# chkconfig --list //3级别已关闭network         0:1:2:3:4:5:6:关[root@cent01 sbin]# chkconfig network on //不输入级别,默认打开2,3,4,5级别[root@cent01 sbin]# chkconfig --listnetwork         0:1:2:3:4:5:6:关[root@cent01 sbin]# chkconfig --del network //删除network[root@cent01 sbin]# chkconfig --list  //network已消失mysqld          0:1:2:3:4:5:6:关netconsole      0:1:2:3:4:5:6:关[root@cent01 sbin]# chkconfig --add network //增加network服务[root@cent01 sbin]# chkconfig --list  //network服务又恢复了mysqld          0:1:2:3:4:5:6:关netconsole      0:1:2:3:4:5:6:关network         0:1:2:3:4:5:6:

systemd

systemd是管理开机启动程序的工具,截止到2015年,大部分linux发行版都已经用systemd取代了SysV管理开机启动项。但是也遭到了一些批评,因为systemd违背了linux系统追求简单的设计思想。

d取自于daemon,表示守护神。systemd的PID为1。

SysV启动开机进程时一次只能启动一个,而systemd则一次可以启动多个服务,这样就导致systemd的开机速度会更快。

 [root@cent01 sbin]# systemctl list-units --all --type=service  //列出系统所有服务  UNIT                      LOAD      ACTIVE   SUB     DESCRIPTION  abrt-ccpp.service         loaded    active   exited  Install ABRT coredump hook  abrt-oops.service         loaded    active   running ABRT kernel log watcher  abrt-vmcore.service       loaded    inactive dead    Harvest vmcores for ABRT  abrt-xorg.service         loaded    inactive dead    ABRT Xorg log watcher  abrtd.service             loaded    active   running ABRT Automated Bug Reporting Tool  accounts-daemon.service   loaded    inactive dead    Accounts Service  alsa-restore.service      loaded    inactive dead    Save/Restore Sound Card State  alsa-state.service         loaded    active   running Manage Sound Card State (restore and store)● apparmor.service          not-found inactive dead    apparmor.service[root@cent01 sbin]# ls /usr/lib/systemd/system  //启动的脚本文件目录abrt-ccpp.service                        iscsid.socket                       saslauthd.serviceabrtd.service                            iscsi.service                       selinux-policy-migrate-local-changes@.serviceabrt-oops.service                        iscsi-shutdown.service              serial-getty@.serviceabrt-pstoreoops.service                  iscsiuio.service                    shutdown.targetabrt-vmcore.service                      iscsiuio.socket                     shutdown.target.wantsabrt-xorg.service                        kdump.service                       sigpwr.targetaccounts-daemon.service                  kexec.target                        sleep.targetalsa-restore.service                     kexec.target.wants                  -.slicealsa-state.service                       kmod-static-nodes.service           slices.targetsystemctl enable crond.service //开机启动 .service可以省略systemctl disable crond.service //禁止开机启动systemctl status crond.service  //查看服务状态systemctl start crond.service //启动服务systemctl stop crond.service  //停止服务systemctl restart crond.service  //重启服务systemctl is-enabled crond.service  //查看某个服务是否开机启动

unit
/usr/lib/systemd/system 此目录下列出了很多文件,这些文件都是unit。
类别如下:

  • service 系统服务
  • target 多个unit组成的组
  • device 硬件设备
  • mount 文件系统挂载点
  • automount 自动挂载点
  • path 文件或路径
  • scope 不是由systemd启动的外部进程
  • slice 进程组
  • snapshot systemd快照
  • socket 进程间通信的套接字
  • swap swap文件
  • timer 定时器
[root@cent01 ~]# systemctl list-units  //列出正在运行的unit[root@cent01 ~]# systemctl list-units --all //列出所有的unit,包括active和inactive[root@cent01 ~]# systemctl list-units --all --state=inactive //列出inactive的unit[root@cent01 ~]# systemctl list-units --all --type=service  //列出所有状态的service[root@cent01 ~]# systemctl list-units --type=service  //列出active的service[root@cent01 ~]# systemctl is-active crond.service  //查看某个unit是否active

target
target类似于centos6的启动级别,target内又包含多个unit的组合,当然target内也可以包含target。启动target就是启动多个unit,用target来管理这些unit。

[root@cent01 ~]# systemctl list-units --all --type=target  //查看当前所有的target  UNIT                   LOAD      ACTIVE   SUB    DESCRIPTION  basic.target           loaded    active   active Basic System  cryptsetup.target      loaded    active   active Encrypted Volumes  emergency.target       loaded    inactive dead   Emergency Mode  final.target           loaded    inactive dead   Final Step  getty.target           loaded    active   active Login Prompts  graphical.target       loaded    inactive dead   Graphical Interface[root@cent01 ~]# systemctl list-dependencies multi-user.target  //查看一个target内包含的所有unitsmulti-user.target● ├─abrt-ccpp.service● ├─abrt-oops.service● ├─abrt-vmcore.service● ├─abrt-xorg.service● ├─abrtd.service● ├─atd.service● ├─auditd.service● ├─avahi-daemon.service[root@cent01 ~]# systemctl get-default  //查看系统默认的targetmulti-user.target[root@cent01 ~]# systemctl set-default multi-user.target  //设置默认的targetRemoved symlink /etc/systemd/system/default.target.Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.

运行级别
multi-user.target等同于centos6的运行级别3。他们的对应关系如下:

SysV运行级别 systemd target 备注 0 poweroff.target 关闭系统 1 rescure.target 单用户模式 2 multiuser.target 用户自定义级别,通常识别为3 3 multiuser.target 多用户命令行模式 4 multiuser.target 用户自定义级别,通常识别为3 5 graphical.target 多用户图形界面,比级别3只多一个GUI 6 reboot.target 重启

所以总结起来,一个service属于一种unit,多个unit组成一个target,当然target里面也可以包含target。

在/usr/lib/systemd/system/sshd.service中有一个字段[install],这里定义了该service属于哪个target。

阅读全文
0 0
原创粉丝点击