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绘制View的过程研究——计算View的大小

    Android绘制View的过程研究——计算View的大小 转自:http://liujianqiao398.blog.163.com/blog/static/18182725720121023218 ...

  2. javabean与map互转

    /**     * 将一个 JavaBean 对象转化为一个  Map     * @param bean 要转化的JavaBean 对象     * @return 转化出来的  Map 对象  * ...

  3. Date and Time Pattern

    The following examples show how date and time patterns are interpreted in the U.S. locale. The given ...

  4. Volume rendering

    Volume rendering Reconstruction filter UCDAVIS

  5. Linux中mod相关的命令 内核模块化 mod相关命令都是用来动态加载内核模块/驱动程序模块

    Linux中mod相关的命令 内核模块化   mod相关命令都是用来动态加载内核模块/驱动程序模块 http://baike.baidu.com/link?url=lxiKxFvYm-UfJIxMjz ...

  6. Centos 6.5系统下搭建Git服务器--失败历程

    参考博客 http://www.51hei.com/bbs/dpj-28077-1.html http://www.linuxidc.com/Linux/2014-06/103885p2.htm ht ...

  7. 推荐几个sql server牛人的博客

    Aaron Bertrand http://sqlblog.com/blogs/aaron_bertrand/ Brent Ozar www.brentozar.com/ Buck Woody htt ...

  8. J2EE MyBatis使用

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .20 ...

  9. 系统性能调优CPU与内存

    CPU相关术语 处理器:插到系统插槽或者处理器版上的物理芯片,以核或者硬件线程的方式包含了一块或者多块CPU. 核:一颗多核处理器上的一个独立CPU实例.核的使用时处理器扩展的一种方式,有称为芯片级多 ...

  10. Leetcode: Guess Number Higher or Lower II

    e are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess ...