我是靠谱客的博主 腼腆薯片,最近开发中收集的这篇文章主要介绍控制服务和守护进程(redhat 8.0)控制服务和守护进程,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

控制服务和守护进程

systemd简介

systemd是用户空间的第一个应用程序,即/sbin/init

init程序的类型:

- SysV风格:init(centos5),实现系统初始化时,随后的初始化操作都是借助于脚本来实现的
特点:
脚本中含有大量的命令,每个命令都要启动一个进程,命令执行完以后就要终止这个进程。如此一来,系统初始化时将大量的创建进程,销毁进程,工作效率会非常低
服务间可能会存在依赖关系,必须严格按照一定的顺序来启动服务,前一个服务没启动完后面的服务就无法执行启动过程。不能并行进行
配置文件:/etc/inittab
- Upstart风格:init(centos6),由ubuntu研发的,通过总线形式以接近于并行的方式工作,效率比SysV高
特点:
基于总线方式能够让进程间互相通信的一个应用程序
不用等服务启动完成,只要一初始化就可以把自己的状态返回给其他进程
配置文件:/etc/inittab,/etc/init/*.conf
- Systemd风格:systemd(centos7)
特点:启动速度比SysV和Upstart都快
不需要通过任何脚本来启动服务,systemd自身就可以启动服务,其本身就是一个强大的解释器,启动服务时不需要sh/bash的参与
systemd不真正在系统初始化时去启动任何一个服务
只要服务没用到,它告诉你启动了,实际上并没有启动。仅当第一次去访问时才会真正启动服务
配置文件:/usr/lib/systemd/system,/etc/systemd/system

systemd的新特性

  • 系统引导时实现服务并行启动
  • 按需激活进程
  • 系统状态快照
  • 基于依赖关系定义服务控制逻辑

Unit

systemd使用unit的概念来管理服务,这些unit表现为一个个配置文件

systemd通过对这些配置文件进行标识和配置达到管理服务的目的:
这些unit文件中包含系统服务、监听socket(套接字)、保存的系统快照以及其他与init相关的信息保存以下目录;

  • /usr/lib/systemd/system
  • /run/systemd/system
  • /etc/systemd/system
    Unit的类型:
Service unit    //文件扩展名为.service,用于定义系统服务
Target unit     //文件扩展名为.target,用于模拟实现“运行级别”
    runlevel0.target和poweroff.target        //关机
    runlevel1.target和rescue.target          //单用户模式
    runlevel2.target和multi-user.target      //对于systemd来说,2/3/4级别没有区别
    runlevel3.target和multi-user.target      //对于systemd来说,2/3/4级别没有区别
    runlevel4.target和multi-user.target      //对于systemd来说,2/3/4级别没有区别
    runlevel5.target和graphical.target       //图形级别
    runlevel6.target和reboot.target          //重启
Device unit     //文件扩展名为.device,用于定义内核识别的设备
Mount unit      //文件扩展名为.mount,用于定义文件系统挂载点
Socket unit     //文件扩展名为.socket,用于标识进程间通信用的socket文件
Snapshot unit   //文件扩展名为.snapshot,用于管理系统快照
Swap unit       //文件扩展名为.swap,用于标识swap设备
Automount unit  //文件扩展名为.automount,用于实现文件系统的自动挂载点
Path unit       //文件扩展名为.path,用于定义文件系统中的一个文件或目录

Unit关键特性

//基于socket的激活机制:
    socket与服务程序分离,当有人去访问时才会真正启动服务,以此来实现按需激活进程与服务的并行启动
//基于bus的激活机制:
    所有使用dbus实现进程间通信的服务,可以在第一次被访问时按需激活
//基于device的激活机制:
    支持基于device激活的系统服务,可以在特定类型的硬件接入到系统中时,按需激活其所需要用到的服务
//基于path的激活机制:
    某个文件路径变得可用,或里面出现新文件时就激活某服务
//系统快照:
    保存各unit的当前状态信息于持久存储设备中,必要时能自动载入
//向后兼容sysv init脚本

使用systemctl管理服务

systemctl command [service name]
重要命令

systemctl start name.service	  #开启服务 systemctl restart name.service #重启服务
systemctl	reload name.service #重新加载服务
systemctl	stop nameservice 	  #停止服务
systemctl	enable name.service #开启服务开机自启
systemctl	disable name.service #关闭服务开机自启

常用命令:

start name.service      //启动服务
stop name.service       //停止服务
restart name.service    //重启服务
status name.service     //查看服务状态
try-restart name.service            //条件式重启服务,若服务已经启动则重启,若服务未启动则不做任何操作
reload-or-restart name.service      //重载或重启服务,能reload则reload,否则restart
reload-or-try-restart name.service  //重载或条件式重启服务,能reload则reload,否则try-restart
mask name.service       //禁止设定为开机自启
unmask name.service     //取消禁止设定为开机自启
list-dependencies name.service      //查看服务的依赖关系
is-active name.service      //查看某服务当前激活与否的状态
is-enabled name.service      //查看服务是否开机自动启动
enable name.service     //设定某服务开机自动启动
disable name.service    //禁止服务开机自动启动
isolate name.target     //切换至某级别,如systemctl isolate graphical.target就是切换至图形界面
list-unit-files --type service      //查看所有服务的开机自动启动状态(是否开机自启)
list-units --type service           //查看所有已经激活的服务状态信息
list-units --type target            //查看所有已装载的级别
list-units --type service --all     //查看所有服务(已启动/已停止)的状态信息
list-units --type target --all     //查看所有的级别
get-default     //查看默认运行级别
set-default name.target     //设置默认运行级别
rescue      //切换至紧急救援模式(大多数服务不启动,但是会加载驱动)
emergency   //切换至emergency模式(驱动不会加载,系统不会初始化,服务不会启动)
halt        //关机
poweroff    //关机
reboot      //重启
suspend     //挂起系统,此时不能关机,否则无用
hibernate   //创建并保存系统快照,下次系统重启时会自动载入快照
hybrid-sleep    //混合睡眠,快照并挂起

systemctl list-dependencies serivicename

#查看服务依赖关系

[root@wcluser ~]# systemctl list-dependencies nginx.service 
nginx.service
● ├─-.mount
● ├─system.slice
● └─sysinit.target
●   ├─dev-hugepages.mount
●   ├─dev-mqueue.mount
●   ├─dracut-shutdown.service
●   ├─import-state.service
●   ├─iscsi.service
●   ├─kmod-static-nodes.service
●   ├─ldconfig.service
●   ├─loadmodules.service
●   ├─lvm2-lvmpolld.socket
●   ├─lvm2-monitor.service
●   ├─multipathd.service
●   ├─nis-domainname.service
nginx.service
● ├─-.mount
● ├─system.slice
● └─sysinit.target
●   ├─dev-hugepages.mount
●   ├─dev-mqueue.mount
●   ├─dracut-shutdown.service
●   ├─import-state.service
●   ├─iscsi.service
●   ├─kmod-static-nodes.service
●   ├─ldconfig.service
●   ├─loadmodules.service
●   ├─lvm2-lvmpolld.socket
●   ├─lvm2-monitor.service
●   ├─multipathd.service
●   ├─nis-domainname.service
●   ├─plymouth-read-write.service
●   ├─plymouth-start.service
●   ├─proc-sys-fs-binfmt_misc.automount
●   ├─rngd.service
●   ├─selinux-autorelabel-mark.service
●   ├─sys-fs-fuse-connections.mount
●   ├─sys-kernel-config.mount
●   ├─sys-kernel-debug.mount
●   ├─systemd-ask-password-console.path
●   ├─systemd-binfmt.service
●   ├─systemd-firstboot.service
●   ├─systemd-hwdb-update.service
●   ├─systemd-journal-catalog-update.service
●   ├─systemd-journal-flush.service
lines 1-30

systemctl list-unit-files --type service

#查看所有服务的开机自启

[root@wcluser ~]# systemctl list-unit-files --type service
UNIT FILE                                  STATE   
arp-ethers.service                         disabled
atd.service                                enabled 
auditd.service                             enabled 
auth-rpcgss-module.service                 static  
autovt@.service                            enabled 
blk-availability.service                   disabled
bolt.service                               static  
chrony-dnssrv@.service                     static  
chrony-wait.service                        disabled
chronyd.service                            enabled 
clean-mount-point@.service                 static  
cockpit-motd.service                       static  
cockpit.service                            static  
console-getty.service                      disabled
container-getty@.service                   static  
cpupower.service                           disabled
crond.service                              enabled 
dbus-org.freedesktop.hostname1.service     static  
dbus-org.freedesktop.locale1.service       static  
dbus-org.freedesktop.login1.service        static  
dbus-org.freedesktop.nm-dispatcher.service enabled 
dbus-org.freedesktop.portable1.service     static  
dbus-org.freedesktop.timedate1.service     enabled 
dbus.service                               static  
debug-shell.service                        disabled
dm-event.service                           static  
dnf-makecache.service                      static  
dracut-cmdline.service                     static  
dracut-initqueue.service                   static  
lines 1-30

systemctl list-units --type service

#查看所有已激活的服务状态信息

[root@wcluser ~]# systemctl list-units --type service
UNIT                       LOAD   ACTIVE SUB     DESCRIPTION              
atd.service                loaded active running Job spooling tools       
auditd.service             loaded active running Security Auditing Service
chronyd.service            loaded active running NTP client/server        
crond.service              loaded active running Command Scheduler        
dbus.service               loaded active running D-Bus System Message Bus 
dracut-shutdown.service    loaded active exited  Restore /run/initramfs on>
getty@tty1.service         loaded active running Getty on tty1            
gssproxy.service           loaded active running GSSAPI Proxy Daemon      
import-state.service       loaded active exited  Import network configurat>
iscsi-shutdown.service     loaded active exited  Logout off all iSCSI sess>
kdump.service              loaded active exited  Crash recovery kernel arm>
kmod-static-nodes.service  loaded active exited  Create list of required s>
libstoragemgmt.service     loaded active running libstoragemgmt plug-in se>
lvm2-monitor.service       loaded active exited  Monitoring of LVM2 mirror>
lvm2-pvscan@259:2.service  loaded active exited  LVM event activation on d>
mcelog.service             loaded active running Machine Check Exception L>
NetworkManager-wait-online.service loaded active exited  Network Manager W>
NetworkManager.service     loaded active running Network Manager          
nfs-idmapd.service         loaded active running NFSv4 ID-name mapping ser>
nfs-mountd.service         loaded active running NFS Mount Daemon         
nfs-server.service         loaded active exited  NFS server and services  
nginx.service              loaded active running The nginx HTTP and revers>
nis-domainname.service     loaded active exited  Read and set NIS domainna>
polkit.service             loaded active running Authorization Manager    
rhsmcertd.service          loaded active running Enable periodic update of>
rngd.service               loaded active running Hardware RNG Entropy Gath>
rpc-statd-notify.service   loaded active exited  Notify NFS peers of a res>
rpc-statd.service          loaded active running NFS status monitor for NF>
rpcbind.service            loaded active running RPC Bind                 
lines 1-30

systemctl list-units --type service --all

#查看所有服务的(运行/停止)状态

[root@wcluser ~]# systemctl list-units --type service
UNIT                       LOAD   ACTIVE SUB     DESCRIPTION              
atd.service                loaded active running Job spooling tools       
auditd.service             loaded active running Security Auditing Service
chronyd.service            loaded active running NTP client/server        
crond.service              loaded active running Command Scheduler        
dbus.service               loaded active running D-Bus System Message Bus 
dracut-shutdown.service    loaded active exited  Restore /run/initramfs on>
getty@tty1.service         loaded active running Getty on tty1            
gssproxy.service           loaded active running GSSAPI Proxy Daemon      
import-state.service       loaded active exited  Import network configurat>
iscsi-shutdown.service     loaded active exited  Logout off all iSCSI sess>
kdump.service              loaded active exited  Crash recovery kernel arm>
kmod-static-nodes.service  loaded active exited  Create list of required s>
libstoragemgmt.service     loaded active running libstoragemgmt plug-in se>
lvm2-monitor.service       loaded active exited  Monitoring of LVM2 mirror>
lvm2-pvscan@259:2.service  loaded active exited  LVM event activation on d>
mcelog.service             loaded active running Machine Check Exception L>
NetworkManager-wait-online.service loaded active exited  Network Manager W>
NetworkManager.service     loaded active running Network Manager          
nfs-idmapd.service         loaded active running NFSv4 ID-name mapping ser>
nfs-mountd.service         loaded active running NFS Mount Daemon         
nfs-server.service         loaded active exited  NFS server and services  
nginx.service              loaded active running The nginx HTTP and revers>
nis-domainname.service     loaded active exited  Read and set NIS domainna>
polkit.service             loaded active running Authorization Manager    
rhsmcertd.service          loaded active running Enable periodic update of>
rngd.service               loaded active running Hardware RNG Entropy Gath>
rpc-statd-notify.service   loaded active exited  Notify NFS peers of a res>
rpc-statd.service          loaded active running NFS status monitor for NF>
rpcbind.service            loaded active running RPC Bind                 
lines 1-30

systemctl list-units --type target --all

#查看所有的级别

[root@wcluser ~]# systemctl list-units --type target --all
  UNIT                      LOAD      ACTIVE   SUB    DESCRIPTION         
  basic.target              loaded    active   active Basic System        
  cryptsetup.target         loaded    active   active Local Encrypted Volu>
  emergency.target          loaded    inactive dead   Emergency Mode      
  getty-pre.target          loaded    inactive dead   Login Prompts (Pre) 
  getty.target              loaded    active   active Login Prompts       
  graphical.target          loaded    inactive dead   Graphical Interface 
  initrd-fs.target          loaded    inactive dead   Initrd File Systems 
  initrd-root-device.target loaded    inactive dead   Initrd Root Device  
  initrd-root-fs.target     loaded    inactive dead   Initrd Root File Sys>
  initrd-switch-root.target loaded    inactive dead   Switch Root         
  initrd.target             loaded    inactive dead   Initrd Default Target
  local-fs-pre.target       loaded    active   active Local File Systems (>
  local-fs.target           loaded    active   active Local File Systems  
  multi-user.target         loaded    active   active Multi-User System   
  network-online.target     loaded    active   active Network is Online   
  network-pre.target        loaded    inactive dead   Network (Pre)       
  network.target            loaded    active   active Network             
  nfs-client.target         loaded    active   active NFS client services 
  nss-lookup.target         loaded    active   active Host and Network Nam>
  nss-user-lookup.target    loaded    active   active User and Group Name >
  paths.target              loaded    active   active Paths               
  remote-fs-pre.target      loaded    active   active Remote File Systems >
  remote-fs.target          loaded    inactive dead   Remote File Systems 
  rescue.target             loaded    inactive dead   Rescue Mode         
  rpc_pipefs.target         loaded    active   active rpc_pipefs.target   
  rpcbind.target            loaded    active   active RPC Port Mapper     
  shutdown.target           loaded    inactive dead   Shutdown            
  slices.target             loaded    active   active Slices              
  sockets.target            loaded    active   active Sockets             
lines 1-30

最后

以上就是腼腆薯片为你收集整理的控制服务和守护进程(redhat 8.0)控制服务和守护进程的全部内容,希望文章能够帮你解决控制服务和守护进程(redhat 8.0)控制服务和守护进程所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部