CentOS7.9 systemctl
命令格式
systemctl [命令] [服务名称]
systemctl start nginx.service
命令
1 start 开启
2 stop 关闭
3 restart 重启
4 status 查看状态
5 is-active 查看激活与否
6 enable 设置开机启动
7 disable 禁止开机启动
8 is-enabled 查看是否开机自启
9 kill 杀死进程
10 mask 禁止自动和手动启动
11 unmask 取消禁止
12 list-dependencies 查看服务的依赖关系
语法
#语法
systemctl COMMAND name.service
#启动
systemctl start name.service
#停止
systemctl stop name.service
#重启
systemctl restart name.service
#查看状态
systemctl status name.service
#禁止自动和手动启动
systemctl mask name.service
#取消禁止
systemctl unmask name.service
#查看某服务当前激活与否的状态:
systemctl is-active name.service
#查看所有已经激活的服务:
systemctl list-units --type|-t service
#查看所有服务:
systemctl list-units --type service --all
#设定某服务开机自启,相当于chkconfig name on
systemctl enable name.service
#设定某服务开机禁止启动:相当于chkconfig name off
systemctl disable name.service
#查看所有服务的开机自启状态,相当于chkconfig --list
systemctl list-unit-files --type service
#用来列出该服务在哪些运行级别下启用和禁用:chkconfig –list name
ls /etc/systemd/system/*.wants/name.service
#查看服务是否开机自启:
systemctl is-enabled name.service
#列出失败的服务
systemctl --failed --type=service
#开机并立即启动或停止
systemctl enable --now postfix
systemctl disable --now postfix
#查看服务的依赖关系:
systemctl list-dependencies name.service
#杀掉进程:
systemctl kill unitname
#重新加载配置文件
systemctl daemon-reload
#关机
systemctl halt、systemctl poweroff
#重启:
systemctl reboot
#挂起:
systemctl suspend
#休眠:
systemctl hibernate
#休眠并挂起:
systemctl hybrid-sleep
加载配置文件
systemctl 名字-reload
关机和开机
systemctl poweroff 关机
systemctl reboot 开机
unit 文件存放位置
本文件一共有三个地方可以存放
/etc/systemd/system/
/usr/lib/systemd/system
/lib/systemd/system
unit 格式说明
1、以 '#' 开头的行后面的内容会被认为是注释
2、相关布尔值,1、yes、on、true 都是开启,0、no、off、false 都是关闭
3、时间单位默认是秒,所以要用毫秒(ms)分钟(m)等须显式说明
service unit file 文件构成部分
1、[Unit]:定义与Unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等
2、[Service]:与特定类型相关的专用选项;此处为Service类型
3、[Install]:定义由“systemctl enable”以及"systemctl disable“命令在实现服务启用或禁用时用到的一些选项
unit 段的常用选项
| 描述 | 可选项 |
|---|---|
| Description | 对当前服务的简单描述 |
| After | 可以指定在哪些服务之后进行启动 |
| Before | 可以指定在哪些服务之前进行启动 |
| Requires | 可以指定服务依赖于哪些服务 (这种依赖是” 强依赖”,一旦所依赖的服务异常,当前的服务也随之停止) |
| Wants | 可以指定服务依赖于哪些服务 (这种依赖是” 弱依赖”,即使所依赖的服务的启动情况不影响当前的服务是否启动) |
| Conflicts | 定义 units 间的冲突关系 |
service 段的常用选项
| 可选项 | 描述 |
|---|---|
| EnvironmentFile | 环境配置文件,用来指定当前服务启动的环境变量 |
| ExecStart | 指定服务启动时执行的命令或脚本 |
| ExecStartPre | 指定服务启动前执行的命令或脚本 |
| ExecStartPost | 指定服务启动后执行的命令或脚本 |
| ExecStop | 指明停止服务要运行的命令或脚本 |
| ExecStopPost | 指定服务停止之后执行的命令或脚本 |
| RestartSec | 指定服务在重启时等待的时间,单位为秒 |
| ExecReload | 指明重启服务要运行的命令或脚本 |
| Restart | 当设定 Restart=1 时,则当次 daemon 服务意外终止后,会再次自动启动此服务,具体看下列类型 |
| PrivateTmp | 设定为 yes 时,会在生成 /tmp/systemd-private-UUID-NAME.service-XXXXX/tmp/ 目录 |
| KillMode | 指定停止的方式,具体见下面 |
| Restart | 指定重启时的类型,具体见下面 |
| Type | 指定启动类型,具体见下面 |
type 选项
| type 可选项 | 描述 |
|---|---|
| simple | 指定 ExecStart 字段的进程为主进程 |
| forking | 指定以 fork () 子进程执行 ExecStart 字段的进程 |
| oneshot | 执行一次 |
| notify | 启动后发送会发送通知信号通知 systemd |
| idle | 等其他任务结束后才运行 |
restart 的可选值
| restart 可选项 | 描述 |
|---|---|
| no | 退出后不会重启 |
| on-success | 当进程正常退出时 (退出码为 0) 执行重启 |
| on-failure | 当进程不正常退出时 (退出码不为 0) 执行重启 |
| on-abnormal | 当被信号终止和超时执行重启 |
| on-abort | 当收到没有捕捉到的信号终止时执行重启 |
| on-watchdog | 当看门狗超时时执行重启 |
| always | 一直重启 |
killModel
| KillMode 可选项 | 描述 |
|---|---|
| control-group | 杀掉当前进程中所有的进程 |
| process | 杀掉当前进程的主进程 |
| mixed | 主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号 |
| none | 不杀掉任何进程 |
install 段的常用选项
| install 段的常用选项 | 描述 |
|---|---|
| Alias | 别名,可使用 systemctl command Alias.service |
| RequiredBy | 被哪些 units 所依赖,强依赖 |
| WantedBy | 被哪些 units 所依赖,弱依赖 |
| Also | 安装本服务的时候还要安装别的相关服务 |
Install 一般填为 WantedBy=multi-user.target
注意:对于新创建的unit文件,或者修改了的unit文件,要通知systemd重载此配置文件,而后可以选择重启,使用命令
systemctl daemon-reload
unit 文件的例子
[Unit]
Description=Frp Client Service #指明自己的描述
After=network.target #指明本服务需要在network服务启动后在启动
[Service]
Type=simple #指明下面ExecStart字段的进程为主进程
User=nobody#这个可写可不写
Restart=always#当进程正常退出时重启
RestartSec=5s #服务在重启时等待的时间,这里指定为5s
ExecStart=/usr/bin/frpc -c /etc/frp/frpc.ini #指定服务启动时运行的脚本或者命令
ExecReload=/usr/bin/frpc reload -c /etc/frp/frpc.ini #指定服务重启时运行的脚本或者命令
LimitNOFILE=1048576 #进程的文件句柄硬限制
[Install]
WantedBy=multi-user.target
将此文件命名为 frps.service 复制到 /lib/systemd/system 里面,然后重新加载 systemctl 配置文件
systemctl daemon-reload
nginx unit 文件
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
Documentation=https://nginx.org/en/
[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t -c /opt/nginx/conf/nginx.conf
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s stop
ExecQuit=/opt/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
supervisord unit 文件
[Unit]
Description=Supervisor daemon
Documentation=http://supervisord.org/
[Service]
Type=forking
ExecStart=/opt/anaconda3/envs/GrowthVision/bin/supervisord -c /etc/supervisord.conf
ExecStop=/opt/anaconda3/envs/GrowthVision/bin/supervisord shutdown
ExecReload=/opt/anaconda3/envs/GrowthVision/bin/supervisord reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
``
CentOS7.9 systemctl的更多相关文章
- CentOS7利用systemctl添加自定义系统服务【转】
systemctl enable name.service 设置开机启 systemctl disable name.service 删除开机启动指令 systemctl list-units --t ...
- CentOS7 tomcat systemctl脚本
1,centos7 使用 systemctl 替换了 service命令 参考:redhat文档: https://access.redhat.com/documentation/en-US/Red_ ...
- [转帖]备忘:CentOS-7 使用systemctl 管理的服务,文件打开数上限1024要改
备忘:CentOS-7 使用systemctl 管理的服务,文件打开数上限1024要改 https://blog.csdn.net/toontong/article/details/50440272 ...
- [转]CentOS7利用systemctl添加自定义系统服务
原文:https://www.cnblogs.com/saneri/p/7778756.html CentOS7自定义系统服务 CentOS7的服务systemctl脚本存放在:/usr/lib/sy ...
- CentOS7利用systemctl添加自定义系统服务
CentOS7的每一个服务以.service结尾,一般会分为3部分:[Unit].[Service]和[Install] 转载于互联网 [Unit] 部分主要是对这个服务的说明,内容包括Descrip ...
- Centos7 自定义systemctl服务脚本
序言篇: 之前工作环境一直使用Centos6版本,脚本一直在使用/etc/init.d/xxx:系统升级到Cento7后,虽然之前的启动脚本也可以使用,但一直没有使用systemctl 的自定义脚本. ...
- CentOS7使用systemctl添加自定义服务
一.简介 Centos7开机第一个程序从init完全换成了systemd这种启动方式,同centos 5 6已经是实质差别.systemd是靠管理unit的方式来控制开机服务,开机级别等功能. 在/u ...
- CentOS7 上systemctl
CentOS 上systemctl 的用法 [日期:--] 来源:Linux社区 作者:Linux [字体:大 中 小] 我们对service和chkconfig两个命令都不陌生,systemctl ...
- Linux学习笔记之Centos7 自定义systemctl服务脚本
0x00 概述 之前工作环境一直使用Centos6版本,脚本一直在使用/etc/init.d/xxx:系统升级到Cento7后,虽然之前的启动脚本也可以使用,但一直没有使用systemctl 的自定义 ...
- 对比centos7的systemctl和其他service+chkconfig
syetemctl就是service和chkconfig这两个命令的整合,在CentOS 7就开始被使用了.systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig ...
随机推荐
- prometheus 监控系统
一. 安装docker环境 二. 安装prometheus 2.1 编辑配置文件 2.2 编辑docker-compose 三. grafana 展示 四 添加监控节点 五. 监控 java进程 六. ...
- vuecli-vite-vue3-init 项目架子 快速开发 webpack打包
要vite的开发的快速 和 webpack打包 开发的时候 用vite,可以打包一个本地可以直接双击,不用起服务的代码 这个架子的缺点就是 vite和vuecli 两套双配置 正式公司项目 还是vue ...
- 记Okhttp的拦截器导致app崩溃问题
原文: 记Okhttp的拦截器导致app崩溃问题 - Stars-One的杂货小窝 app对接的后台接口,传参需要加密,获取数据需要解密,于是使用了拦截器去实现,然后发现以下问题: 即使在发起请求的那 ...
- [Raspberry Pi]树莓派多线程下串口收发数据
[Raspberry Pi]树莓派多线程下串口收发数据 鼠鼠用的是python开发树莓派,因为python是最优美的语言! 少废话,直接上代码: import threading import ser ...
- C#中的For与Foreach循环:一场性能对话与实战解析
引言 在C#编程实践中,选择适当的循环结构对程序性能至关重要,尤其是在处理大量数据或追求极致运行效率时.本文将深入探讨C#中的两种主要迭代机制--传统的for循环和基于集合迭代器的foreach循环之 ...
- 记录--vue3 + mark.js | 实现文字标注功能
这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 页面效果 具体实现 新增 1.监听鼠标抬起事件,通过window.getSelection()方法获取鼠标用户选择的文本范围或光标的当前位 ...
- 使用Go语言开发一个短链接服务:一、基本原理
章节 使用Go语言开发一个短链接服务:一.基本原理 使用Go语言开发一个短链接服务:二.架构设计 使用Go语言开发一个短链接服务:三.项目目录结构设计 使用Go语言开发一个短链接服务:四.生成 ...
- FPGA中的速度优化
FPGA中的速度优化 一.逻辑设计中的速度概念 逻辑设计速度相关的概念有三个:设计吞吐量.设计延时和设计时序.速度优化策略而言,吞吐量需要提高,延时应该降低,时序应该收敛(时序余量slave越大,收敛 ...
- KingbaseES V8R6集群运维系列 -- connect_check_type参数
案例说明: 在KingbaseES V8R6C7的版本中,repmgr.conf增加了connect_check_type参数可以指定参数值,默认值是'mix',以前版本为隐藏参数,默认值'ping' ...
- 跳转到制定Sheet页及提交指定sheet页内容
一.跳转到指定Sheet的实现 话不多说,先上效果图 两个按钮的事件分别如下: _g().loadSheetByName("sheet1") # 跳转至sheet1按钮事件 _g( ...