第八章 系统服务和守护进程管理



8.1 了解自启动系统服务


在RHEL7中,系统启动和服务进程都是有systemd管理.这个systemd负责在系统启动或系统运行过程中,激活系统资源,服务守护进程和其他进程.

systemd取代了RHEL家族中以往的System V 和 init程序启动系统的做法,它的最终目标是提供更优秀的框架以表示系统服务之间的依赖关系,并以此达到提供系统初始化时服务的并行启动效率,降低shell脚本的使用和系统开销的效果

systemd使用服务单元为基本单位管理系统的启动.



systemd服务管理:  服务的启动脚本被服务单元取代,服务单元以.service文件扩展结束,提供了与服务启动脚本一样的功能.服务单元为systemd读取的配置文件.

systemctl is-enabled sth.service 查询服务是否开机启动

systemctl is-active sth.service 查询服务是否在运行中

systemctl status sth.service 查询服务运行状态

systemctl --failed --tyoe=service 查询启动失败的服务


systemctl list-units --type=service 列出所有已经加载并激活(运行中)的unit , --type 指定unit的类别, --all列出所有的unit,包括非活动状态的(非运行中)


systemctl list-unit-files --type=service  列出所有unit的开机启动设置(相当于以前的chkconfig --list)

systemctl list-dependencies [sth.unit]



服务状态说明:

loaded 服务单元的配置文件已经被处理

active(running) 服务的一个或多个进程在运行中

active(exited) 某些一次性运行的服务已经成功被执行并退出(一次性服务:服务运行后完成任务,相关进程会自动退出)

active(waiting) 服务已经运行但在等待某个事件

inactive 服务没有在运行

enabled 服务设定为开机运行

disabled 服务设定为开机不运行

static 服务不能被设定开机启动,但可以由其他服务启动该服务



8.2 启动和控制系统服务


systemctl enable sth.service 开机自动运行该服务

systemctl disable sth.service 开机取消运行该服务(但用户可以在后期手工启动该服务)

systemctl start sth.service 马上启动服务

systemctl stop sth.service 马上停止服务

systemctl restart sth.service 重启服务

systemctl reload sth.service 重载服务

systemctl mask sth.serivce 禁止服务被运行(开机启动和手工后期启动服务也将会被禁止,当前已经运行的服务当次不马上受影响)

systemctl umask sth.service 取消禁止服务运行







笔记:


[student@localhost Desktop]$ systemctl is-enabled sshd  查看是否开机启动

enabled

[student@localhost Desktop]$ systemctl is-active sshd  现在是否是活动的

active

[student@localhost Desktop]$ systemctl status sshd

sshd.service - OpenSSH server daemon

   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)

   Active: active (running) since Thu 2017-05-25 16:52:18 CST; 1 weeks 1 days ago

  Process: 1528 ExecStartPre=/usr/sbin/sshd-keygen (code=exited, status=0/SUCCESS)

 Main PID: 1547 (sshd)

   CGroup: /system.slice/sshd.service

           └─1547 /usr/sbin/sshd -D


[root@desktop0 ~]# systemctl disable sshd                            开机不启动  不改变当前状态

rm '/etc/systemd/system/multi-user.target.wants/sshd.service'

[root@desktop0 ~]# systemctl enable sshd                       开机启动

ln -s '/usr/lib/systemd/system/sshd.service' '/etc/systemd/system/multi-user.target.wants/sshd.service'







[root@desktop0 ~]# systemctl reload sshd  重载入,进程不会断

Job for sshd.service failed. See 'systemctl status sshd.service' and 'journalctl -xn' for details.

[root@desktop0 ~]# systemctl restart sshd    重启,进程会断,