Linux Systemctl是一个系统管理守护进程、工具和库的集合,用于取代System V、service和chkconfig命令,初始进程主要负责控制systemd系统和服务管理器。通过Systemctl –help可以看到该命令主要分为:查询或发送控制命令给systemd服务,管理单元服务的命令,服务文件的相关命令,任务、环境、快照相关命令,systemd服务的配置重载,系统开机关机相关的命令。

  1. 列出所有可用单元

  # systemctl list-unit-files

  2. 列出所有运行中单元

  # systemctl list-units

  3. 列出所有失败单元

  # systemctl –failed

  4. 检查某个单元(如 crond.service)是否启用

  # systemctl is-enabled crond.service

  5. 列出所有服务

  # systemctl list-unit-files –type=service

  6. Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态

  # systemctl start httpd.service

  # systemctl restart httpd.service

  # systemctl stop httpd.service

  # systemctl reload httpd.service

  # systemctl status httpd.service

  注意:当我们使用systemctl的start,restart,stop和reload命令时,终端不会输出任何内容,只有status命令可以打印输出。

  7. 如何激活服务并在开机时启用或禁用服务(即系统启动时自动启动mysql.service服务)

  # systemctl is-active mysql.service

  # systemctl enable mysql.service

  # systemctl disable mysql.service

  8. 如何屏蔽(让它不能启动)或显示服务(如ntpdate.service)

  # systemctl mask ntpdate.service

  ln -s ‘/dev/null”/etc/systemd/system/ntpdate.service’

  # systemctl unmask ntpdate.service

  rm ‘/etc/systemd/system/ntpdate.service’

  9. 使用systemctl命令杀死服务

  # systemctl kill crond

  10. 列出所有系统挂载点

  # systemctl list-unit-files –type=mount

  11. 挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态

  # systemctl start tmp.mount

  # systemctl stop tmp.mount

  # systemctl restart tmp.mount

  # systemctl reload tmp.mount

  # systemctl status tmp.mount

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

  # systemctl is-active tmp.mount

  # systemctl enable tmp.mount

  # systemctl disable tmp.mount

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

  # systemctl mask tmp.mount

  ln -s ‘/dev/null”/etc/systemd/system/tmp.mount’

  # systemctl unmask tmp.mount

  rm ‘/etc/systemd/system/tmp.mount’

  14. 列出所有可用系统套接口

  # systemctl list-unit-files –type=socket

  15. 检查某个服务的所有配置细节

  # systemctl show mysql

  16. 获取某个服务(httpd)的依赖性列表

  # systemctl list-dependencies httpd.service

  17. 启动救援模式

  # systemctl rescue

  18. 进入紧急模式

  # systemctl emergency

  19. 列出当前使用的运行等级

  # systemctl get-default

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

  # systemctl isolate runlevel5.target

  或

  # systemctl isolate graphical.target

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

  # systemctl isolate runlevel3.target

  或

  # systemctl isolate multiuser.target

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

  # systemctl set-default runlevel3.target

  # systemctl set-default runlevel5.target

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

  # systemctl reboot

  # systemctl halt

  # systemctl suspend

  # systemctl hibernate

  # systemctl hybrid-sleep

  对于不知运行等级为何物的人,说明如下。

  Runlevel 0 : 关闭系统

  Runlevel 1 : 救援,维护模式

  Runlevel 3 : 多用户,无图形系统

  Runlevel 4 : 多用户,无图形系统

  Runlevel 5 : 多用户,图形化系统

  Runlevel 6 : 关闭并重启机器

CentOS 7继承了RHEL 7的新的特性,例如强大的systemctl,而systemctl的使用也使得以往系统服务的/etc/init.d的启动脚本的方式就此改变,也大幅提高了系统服务的运行效率。但服务的配置和以往也发生了极大的不同,说实在的,变的简单而易用了许多。

下面我们以利用forever来实现Node.js项目自启动为例,初探CentOS 7的systemctl。

前提:Node.js环境已配置成功,forever包安装成功,有一个能跑的Node.js程序。

CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,还是存在系统服务里吧,即:/usr/lib/systemd/system目录下

每一个服务以.service结尾,一般会分为3部分:[Unit]、[Service]和[Install],我写的这个服务用于开机运行Node.js项目,具体内容如下:

[Unit]
Description=xiyoulibapi
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/node.js/pid
ExecStart=/usr/local/bin/forever start /node.js/xiyoulib/bin/www
ExecReload=/usr/local/bin/forever restart /node.js/xiyoulib/bin/www
ExecStop=/usr/local/bin/forever stop /node.js/xiyoulib/bin/www
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

[Unit]部分主要是对这个服务的说明,内容包括Description和After,Description用于描述服务,After用于描述服务类别

[Service]部分是服务的关键,是服务的一些具体运行参数的设置,这里Type=forking是后台运行的形式,PIDFile为存放PID的文件路径,ExecStart为服务的具体运行命令,ExecReload为重启命令,ExecStop为停止命令,PrivateTmp=True表示给服务分配独立的临时空间,注意:[Service]部分的启动、重启、停止命令全部要求使用绝对路径,使用相对路径则会报错!

[Install]部分是服务安装的相关设置,可设置为多用户的

服务脚本按照上面编写完成后,以754的权限保存在/usr/lib/systemd/system目录下,这时就可以利用systemctl进行配置了

首先,使用systemctl start [服务名(也是文件名)]可测试服务是否可以成功运行,如果不能运行则可以使用systemctl status [服务名(也是文件名)]查看错误信息和其他服务信息,然后根据报错进行修改,直到可以start,如果不放心还可以测试restart和stop命令。

接着,只要使用systemctl enable xxxxx就可以将所编写的服务添加至开机启动即可。

我的脚本编写方法参照了nginx的编写方法,也可以根据其他功能类似的程序。

这样看来,虽然systemctl比较陌生,但是其实比init.d那种方式简单不少,而且使用简单,systemctl能简化的操作还有很多,现在也有不少的资料,看来RHEL/CentOS比其他的Linux发行版还是比较先进的,此次更新也终于舍弃了Linux 2.6内核,无论是速度还是稳定性都提升不少。

centos 7 systemctl的更多相关文章

  1. CentOS使用systemctl daemon-reload报错Error getting authority: Error initializing authority: Error calling StartServiceByName for org.freedesktop.PolicyKit1: Timeout was reached (g-io-error-quark, 24)解决办法

    CentOS修改了系统启动文件后需要重载报错 systemctl daemon-reload Error getting authority: Error initializing authority ...

  2. Centos 7 systemctl和防火墙firewalld命令

    今天自己在Hyper-v下搭建三台Linux服务器集群,用于学习ELKstack(即大数据日志解决技术栈Elasticsearch,Logstash,Kibana的简称),下载的Linux版本为cen ...

  3. 【Linux笔记】CentOS 7 systemctl、firewalld

    一.CentOS7 systemctl 在CentOS7中,进行chkconfig命令操作时会发现有类似“systemctl.....”的提示,systemctl可以简单实现service和chkco ...

  4. Docker下使用centos无法使用systemctl怎么办

    提交正在使用的容器: docker commit [ContainerId] 提交停止正在运行无法使用Systemctl的容器: docker stop [ContainerId] 删除这个容器(可选 ...

  5. 关于centos 7 systemctl自定义服务笔记

    通过添加 Restart=always RestartSec=2s StartLimitBurst=10 实现systemctl服务崩溃自动重启

  6. 在centOS使用systemctl配置启动多个tomcat

    公司服务器使用的是阿里云CentOS7,CentOS7和CentOS6目前最大区别就是service变成了现在的systemctl,简单的查了一下并结合使用,发现systemctl功能上等同于6上面的 ...

  7. .netcore centos配置systemctl自动启动

    systemd分两种服务系统和用户服务 对应存储位路径为系统(/usr/lib/systemd/system).用户(/etc/systemd/user/) [Unit] Description=ap ...

  8. Linux——CentOS 7 systemctl和防火墙firewalld命令

    一.防火墙的开启.关闭.禁用命令 (1)设置开机启用防火墙:systemctl enable firewalld.service (2)设置开机禁用防火墙:systemctl disable fire ...

  9. Centos 使用Systemctl报Error getting authority: Error initializing authority: Error calling StartServiceByName for org.freedesktop.PolicyKit1: Timeout was reached (g-io-error-quark, 24)

    在使用centos7.4 安装服务的时候报错: Error getting authority: Error initializing authority: Error calling StartSe ...

随机推荐

  1. Excel 的一些用法--行号赋给一列

    1.鼠标选定要赋值的列 2.在 输入 =Row() 3. Ctrl+Enter

  2. MFC之向导页、消息框、文件选择、字体、颜色(三)

    属性页对话框的分类 属性页对话框想必大家并不陌生,XP系统中桌面右键点属性,弹出的就是属性页对话框,它通过标签切换各个页面.另外,我们在创建MFC工程时使用的向导对话框也属于属性页对话框,它通过点击“ ...

  3. jquery中奖实例代码

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. OpenGl的glMatrixMode()函数理解

    glMatrixMode()函数的参数,这个函数其实就是对接下来要做什么进行一下声明,也就是在要做下一步之前告诉计算机我要对“什么”进行操作了,这个“什么”在glMatrixMode的“()”里的选项 ...

  5. asp.net 项目在 IE 11 下出现 “__doPostBack”未定义 的解决办法

    最近项目在 IE 11 下<asp:LinkButton> 点击出现 “__doPostBack”未定义”,经过一番google,终于知道了原因:ASP.NET 可能无法辨识出一些浏览器的 ...

  6. MVC项目实践,在三层架构下实现SportsStore-01,EF Code First建模、DAL层等

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  7. window.location.href和window.open的几种用法和区别

    使用js的同学一定知道js的location.href的作用是什么,但是在js中关于location.href的用法究竟有哪几种,究竟有哪些区别,估计很多人都不知道了. 一.location.href ...

  8. 第七篇 Integration Services:中级工作流管理

    本篇文章是Integration Services系列的第七篇,详细内容请参考原文. 简介在上一篇文章,我们创建了一个新的SSIS包,学习了SSIS中的脚本任务和优先约束,并检查包的MaxConcur ...

  9. iOS SQLite增删改查(简单应用)

    // 注意: 在工程里导入libsqlite3.tbd库(Xcode7,如果Xcode7以下的版本则导入libsqlite3.dylib). #import <UIKit/UIKit.h> ...

  10. WCF笔记

    http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iparameterinspector.aftercall ...