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. 【Android Studio快捷键】之导入相应包声明(import packages)

    可能import 单个声明的快捷键大家都很容易找到,Alt+Enter.但是如果我要一次性import文件中所有的声明,这个快捷键是什么呢,找啊找的,就是没找到,以前在Eclipse是Ctrl+1,但 ...

  2. nginx支持cgi(c,c++)

    前段时间用我修改了tinyhttpd,让其cgi支持文件流,感觉满小巧,就应用上了.最近访问请求量上来而来,它对socket的各种异常状态处理不好,对于慢速的链接会占用我的线程.虽然我一直想仿出ten ...

  3. js - 驼峰命名

    1. // 驼峰命名 console.log(hump('border-bottom-color')) function hump( str) { if (typeof str != 'string' ...

  4. 我的工具箱之MySql Front 5.3

    下载地址:http://pan.baidu.com/s/1i4sJpNB 这款软件用来连接MySql,作为前端使用. 它功能全面,方便快捷,如果说有缺点的话,sql窗口中不能执行选择的部分有遗憾. 2 ...

  5. textarea 默认文字获取焦点失去焦点

    <textarea name="textarea" cols="" title="contactForm" class="t ...

  6. os模块 关于路径问题使用

    os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.curd ...

  7. 用仿ActionScript的语法来编写html5——第八篇,图片处理+粒子效果

    用仿ActionScript的语法来编写html5系列开发到现在,应该可以做出一些东西了,下面先来研究下图片的各种效果预览各种效果看下图效果和代码看这里,看不到效果的请下载支持html5的浏览器 ht ...

  8. iOS - (利用/调用系统定位获取当前经纬度与地理信息)

    这些天做iOS项目的时候,需要通过定位来拿到当期城市的名称.百度地图SDK有这个功能,但为了不依赖第三方,这里使用iOS自带框架CoreLocation来实现这个需求.iOS8出来之后,针对定位需要多 ...

  9. Feature Scaling

    定义:Feature scaling is a method used to standardize the range of independent variables or features of ...

  10. PostgreSQL 三节点集群故障模拟及恢复

    PostgreSQL 三节点集群故障模拟及恢复 (postgreSQL9.5.1) 正常状态: 10.2.208.10:node1:master 10.2.208.11:node2:standby1同 ...