设为首页 收藏本站
查看: 823|回复: 0

[经验分享] centos7 中 systemd systemctl管理服务的命令

[复制链接]

尚未签到

发表于 2018-4-24 12:42:16 | 显示全部楼层 |阅读模式
  背景: centos7 和 6的重大改变
  对于系统管理员而言,系统的服务管理是一件很日常和很重要的工作,而7在6的基础上有了很大的改变,就连命令都完全不一样了。所以要拥抱变化,学习7是如何进行服务的管理和控制的。
  

  

  system和systemctl的初探
  Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。
  Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。
  在Linux生态系统中,Systemd被部署到了大多数的标准Linux发行版中,只有为数不多的几个发行版尚未部署。Systemd通常是所有其它守护进程的父进程,但并非总是如此。
  

  【第一步】
  查看systemd的版本
  # systemctl --version
  systemd 219
  +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
  

  查看进程是否存在
  ps -ef | grep ystemd
  

  注意:systemd是作为父进程(pid=1)运行的
  

  分析systemd的启动进程
  # systemd-analyze
  Startup finished in 639ms (kernel) + 931ms (initrd) + 12.432s (userspace) = 14.003s
  

  分析各个进程启动发费的时间
  #systemd-analyze blame
  

  分析启动时候的关键链
  systemd-analyze critical-chain
  

  重要:Systemctl接受服务(.service),挂载点(.mount),套接口(.socket)和设备(.device)作为单元。
  

  

  列出所有服务可用单元
  systemctl list-unit-files
  

  (200多个)
  

  列出所有运行中的单元
  systemctl list-units
  

  列出失败的单元?
  systemctl --failed
  

  列出某个单元是否启动
  # systemctl is-enabled crond.service
  enabled
  或者
  # systemctl is-enabled crond
  enabled
  

  检查某个单元或服务是否运行
  

  

  # systemctl is-active crond
  active
  或者
  # systemctl status crond
  这个信息更详细
  

  ############################# 控制服务  ############################
  列出所有服务(包括启用的和禁用的)
  systemctl list-unit-files  --type=service
  (120+)
  

  以httpd为例
  yum install httpd
  会生成以下文件
  /usr/lib/systemd/system/httpd.service
  

  

  Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态
[root@Centos7-node2 ~]# systemctl status httpd

  ● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Active: inactive (dead)
  Docs: man:httpd(8)
  man:apachectl(8)
[root@Centos7-node2 ~]# systemctl start httpd

[root@Centos7-node2 ~]# systemctl status httpd

  ● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Active: active (running) since Sun 2016-10-23 00:48:43 CST; 3s ago
  Docs: man:httpd(8)
  man:apachectl(8)
  Main PID: 26811 (httpd)
  Status: "Processing requests..."
  CGroup: /system.slice/httpd.service
  ├─26811 /usr/sbin/httpd -DFOREGROUND
  ├─26812 /usr/sbin/httpd -DFOREGROUND
  ├─26813 /usr/sbin/httpd -DFOREGROUND
  ├─26814 /usr/sbin/httpd -DFOREGROUND
  ├─26815 /usr/sbin/httpd -DFOREGROUND
  └─26816 /usr/sbin/httpd -DFOREGROUND
  

  Oct 23 00:48:17 Centos7-node2 systemd[1]: Starting The Apache HTTP Server...
  Oct 23 00:48:33 Centos7-node2 httpd[26811]: AH00558: httpd: Could not reliably determine the server's fully qua...ssage
  Oct 23 00:48:43 Centos7-node2 systemd[1]: Started The Apache HTTP Server.
  Hint: Some lines were ellipsized, use -l to show in full.
[root@Centos7-node2 ~]# systemctl reload httpd

[root@Centos7-node2 ~]# systemctl stop httpd

  

  

  

  注意:
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  

  在loader中第一个disabled表示系统启动是否自启动,为非自动。 第二个不知道干嘛
  

  Active: active (running) since Sun 2016-10-23 00:48:43 CST; 3s ago
  

  Active 表示服务是不是在运行
  

  

  如何激活服务并在启动时启用或禁用服务(即系统启动时自动启动服务)
[root@Centos7-node2 ~]# systemctl enable httpd

  Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
  

  禁用
  systemctl disabled httpd
[root@Centos7-node2 ~]# systemctl disable httpd

  Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
  

  

  使用systemctl命令杀死服务
[root@Centos7-node2 ~]# systemctl kill httpd

[root@Centos7-node2 ~]# systemctl status httpd

  ● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Active: failed (Result: exit-code) since Sun 2016-10-23 00:56:59 CST; 1s ago
  Docs: man:httpd(8)
  man:apachectl(8)
  Process: 26901 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
  Main PID: 26869 (code=exited, status=0/SUCCESS)
  Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
  

  Oct 23 00:51:38 Centos7-node2 systemd[1]: Starting The Apache HTTP Server...
  Oct 23 00:51:53 Centos7-node2 httpd[26869]: AH00557: httpd: apr_sockaddr_info_get() failed for Centos7-node2
  Oct 23 00:51:53 Centos7-node2 httpd[26869]: AH00558: httpd: Could not reliably determine the server's fully qua...ssage
  Oct 23 00:52:03 Centos7-node2 systemd[1]: Started The Apache HTTP Server.
  Oct 23 00:56:59 Centos7-node2 kill[26901]: kill: cannot find process ""
  Oct 23 00:56:59 Centos7-node2 systemd[1]: httpd.service: control process exited, code=exited status=1
  Oct 23 00:56:59 Centos7-node2 systemd[1]: Unit httpd.service entered failed state.
  Oct 23 00:56:59 Centos7-node2 systemd[1]: httpd.service failed.
  Hint: Some lines were ellipsized, use -l to show in full.
  

  

  注意:Active: failed (Result: exit-code) since Sun 2016-10-23 00:56:59 CST; 1s ago
  这个的failed表示的是kill掉的?
  

  

  

  ##########################  使用Systemctl控制并管理挂载点 #################3
[root@Centos7-node2 ~]# systemctl list-unit-files --type=mount

  UNIT FILE                     STATE
  dev-hugepages.mount           static
  dev-mqueue.mount              static
  proc-sys-fs-binfmt_misc.mount static
  sys-fs-fuse-connections.mount static
  sys-kernel-config.mount       static
  sys-kernel-debug.mount        static
  tmp.mount                     disabled
  

  

  挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态
  systemctl start tmp.mount
  systemctl status tmp.mount
  systemctl reload tmp.mount
  

  在启动时激活、启用或禁用挂载点(系统启动时自动挂载)
  

  # systemctl is-active tmp.mount
  # systemctl enable tmp.mount
  # systemctl disable tmp.mount
  

  在Linux中屏蔽(让它不能启用)或可见挂载点
  

  # systemctl mask tmp.mount
  ln -s '/dev/null''/etc/systemd/system/tmp.mount'
  # systemctl unmask tmp.mount
  rm '/etc/systemd/system/tmp.mount'
  

  

  ####################  控制系统运行等级  ##################
  

  

  启动系统救援模式
  

  # systemctl rescue
  Broadcast message from root@tecmint on pts/0(Wed2015-04-2911:31:18 IST):
  The system is going down to rescue mode NOW!
  

  进入紧急模式
  

  # systemctl emergency
  Welcome to emergency mode!After logging in, type "journalctl -xb" to view
  system logs,"systemctl reboot" to reboot,"systemctl default" to try again
  to boot intodefault mode.
  列出当前使用的运行等级
  

  注意:init 1也是可以使用的
  # systemctl get-default
  multi-user.target
  注意:who -r 也是可以查看的
  

  启动运行等级5,即图形模式
  

  # systemctl isolate runlevel5.target
  或
  # systemctl isolate graphical.target
  

  启动运行等级3,即多用户模式(命令行)
  

  # systemctl isolate runlevel3.target
  或
  # systemctl isolate multiuser.target
  

  设置多用户模式或图形模式为默认运行等级
  

  # systemctl set-default runlevel3.target
  # systemctl set-default runlevel5.target
  

  重启、停止、挂起、休眠系统或使系统进入混合睡眠
  

  # systemctl reboot
  # systemctl halt
  # systemctl suspend
  # systemctl hibernate
  # systemctl hybrid-sleep
  对于不知运行等级为何物的人,说明如下。
  

  Runlevel 0 : 关闭系统
  Runlevel 1 : 救援?维护模式
  Runlevel 3 : 多用户,无图形系统
  Runlevel 4 : 多用户,无图形系统
  Runlevel 5 : 多用户,图形化系统
  Runlevel 6 : 关闭并重启机器
  

  

  注意:在centos7 中仍然可以使用init 0 关机 init 6 启动。   
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-451413-1-1.html 上篇帖子: centos 7 systemctl自动补全命令插件 下篇帖子: CentOS7.3系统优化
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表