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. 浅谈 man 命令的日常使用

    Linux系统提供了相对比较丰富的帮助手册(man),man是manual的缩写,在日常linux系统管理中经常用到,今天就简单聊聊man.man 本身也提供自己的帮助手册,通过man就可以查看. ( ...

  2. 微信连Wi-Fi专业设备列表

    微信连Wi-Fi是微信推出的快速连接Wi-Fi热点的功能.商户启用后,其顾客仅需通过微信“扫一扫”二维码等方式,即可快速连接商户提供的Wi-Fi免费上网.连接成功后,用户微信主界面顶部会出现“正在连接 ...

  3. [BS-11] 关于RGB/ARGB颜色相关知识

    关于RGB/ARGB颜色相关知识 众所周知,自然界的颜色都是由红色R.绿色G.蓝色B三元色按不同比例混合而成,每种元色取值范围是0-255.iOS中图片的颜色分为2种:24位和32位. 1. 24位( ...

  4. Java控制语句——while语句

    while循环 在循环刚开始时,会计算一次“布尔表达式”的值,若条件为真,执行循环体,而对于后来每一次额外的循环,都会在开始前重新计算一次. 注意:语句中应有使循环趋向于结束的语句,否则会出现无限循环 ...

  5. vmware虚拟机三种网络模式详解_转

    原文来自http://note.youdao.com/share/web/file.html?id=236896997b6ffbaa8e0d92eacd13abbf&type=note 由于L ...

  6. C# 调用VC++的DLL,VC++封装DLL

    VS中新建一个动态库项目 文件生成一个工程名对应的.cpp文件,该文件定义 DLL应用程序的导出函数. 工程内新建一个类OutputInt,我用类向导生成,工程中会添加OutputInt.cpp和Ou ...

  7. Git撤销操作命令

    使用Git的过程中,失误无法避免,虽然Git很强,但是有些失误,无法挽回.在这里我介绍一些Git的撤销命令. 撤销对文件的修改 如下图所示的情况,你修改了文件,但是不想保存对文件的修改. 根据具体情况 ...

  8. weblogic对jms实现的QueueConnection实现与TopicConnection实现问题

    今天看了一段之前同事写jms的代码,觉得好像不对,但是不可能,生产上用的代码.刚开始想了下,脑子没转过弯来,后来一想是个简单的问题 代码如下: topicConnection = (TopicConn ...

  9. android studio ,Gradle DSL method not found: 'compile()'

    用gradle构建android工程出现  Gradle DSL method not found: 'compile()' 错误 检查你外层的build.gradle文件中是不是用了compile方 ...

  10. Java基础之读文件——使用输入流读取二进制文件(StreamInputFromFile)

    控制台程序,读取Java基础之读文件部分(StreamOutputToFile)写入的50个fibonacci数字. import java.nio.file.*; import java.nio.* ...