centos 7 systemctl
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的更多相关文章
- 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 ...
- Centos 7 systemctl和防火墙firewalld命令
今天自己在Hyper-v下搭建三台Linux服务器集群,用于学习ELKstack(即大数据日志解决技术栈Elasticsearch,Logstash,Kibana的简称),下载的Linux版本为cen ...
- 【Linux笔记】CentOS 7 systemctl、firewalld
一.CentOS7 systemctl 在CentOS7中,进行chkconfig命令操作时会发现有类似“systemctl.....”的提示,systemctl可以简单实现service和chkco ...
- Docker下使用centos无法使用systemctl怎么办
提交正在使用的容器: docker commit [ContainerId] 提交停止正在运行无法使用Systemctl的容器: docker stop [ContainerId] 删除这个容器(可选 ...
- 关于centos 7 systemctl自定义服务笔记
通过添加 Restart=always RestartSec=2s StartLimitBurst=10 实现systemctl服务崩溃自动重启
- 在centOS使用systemctl配置启动多个tomcat
公司服务器使用的是阿里云CentOS7,CentOS7和CentOS6目前最大区别就是service变成了现在的systemctl,简单的查了一下并结合使用,发现systemctl功能上等同于6上面的 ...
- .netcore centos配置systemctl自动启动
systemd分两种服务系统和用户服务 对应存储位路径为系统(/usr/lib/systemd/system).用户(/etc/systemd/user/) [Unit] Description=ap ...
- Linux——CentOS 7 systemctl和防火墙firewalld命令
一.防火墙的开启.关闭.禁用命令 (1)设置开机启用防火墙:systemctl enable firewalld.service (2)设置开机禁用防火墙:systemctl disable fire ...
- 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 ...
随机推荐
- RaspberryPi uart
通过调试口查看树莓派开机启动信息,学习python控制串口的方法. 参考链接: http://www.elinux.org/Serial_port_programming 硬件连接: 硬件原理图链接: ...
- JS-JQ实现TAB选项卡
原理: 有两种实现方法, 方法一利用css的display:none 和display:block:交替实现: 方法二利用css的z-index:
- select下拉框美化
其实用下列CSS就可以解决,原理是将浏览器默认的下拉框样式清除,然后应用上自己的,再附一张向右对齐小箭头的图片即可. select { /*Chrome和Firefox里面的边框是不一样的,所以复 ...
- UVA 10127题目的解答
#include <iostream>#include <cstdio>#include <cmath> int main(){ int num; while (s ...
- Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete .
String url = "http://www.baidu.com"; //将要访问的url字符串放入HttpPost中 HttpPost httpPost= new HttpP ...
- XUtils框架中HttpUtils使用Get请求时总是返回相同信息的问题解决,xutilshttputils
如需转载请标明出处:http://blog.csdn.net/itas109 版本:Xutils 2014年11月11日 下载地址:https://github.com/wyouflf/xUtils ...
- C#实现根据IP 查找真实地址
IPScanner.cs public class IPScanner { private byte[] data; Regex regex = new Regex(@"(((\d{1,2} ...
- js星级评分点击星级评论打分效果
html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- 网卡流量查看软件bmon
bmon 时 linux下最常用的查看网络带宽的工具,debian下直接进行安装即可 apt-get install bmon redhat下可以在这里寻找到合适版本的rpm包,安装完毕后执行bmon ...
- FAQ: c++ 函数名后添加 const void function1(int &id) const
说明这个函数不能修改这个类的成员变量!只能操作自己的参数和内部的范围变量! 括号内的&id,&表示这个变量和C# in和out是一样的,算是一个reference,可以更改值,要想不更 ...