命令格式

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的更多相关文章

  1. CentOS7利用systemctl添加自定义系统服务【转】

    systemctl enable name.service 设置开机启 systemctl disable name.service 删除开机启动指令 systemctl list-units --t ...

  2. CentOS7 tomcat systemctl脚本

    1,centos7 使用 systemctl 替换了 service命令 参考:redhat文档: https://access.redhat.com/documentation/en-US/Red_ ...

  3. [转帖]备忘:CentOS-7 使用systemctl 管理的服务,文件打开数上限1024要改

    备忘:CentOS-7 使用systemctl 管理的服务,文件打开数上限1024要改 https://blog.csdn.net/toontong/article/details/50440272 ...

  4. [转]CentOS7利用systemctl添加自定义系统服务

    原文:https://www.cnblogs.com/saneri/p/7778756.html CentOS7自定义系统服务 CentOS7的服务systemctl脚本存放在:/usr/lib/sy ...

  5. CentOS7利用systemctl添加自定义系统服务

    CentOS7的每一个服务以.service结尾,一般会分为3部分:[Unit].[Service]和[Install] 转载于互联网 [Unit] 部分主要是对这个服务的说明,内容包括Descrip ...

  6. Centos7 自定义systemctl服务脚本

    序言篇: 之前工作环境一直使用Centos6版本,脚本一直在使用/etc/init.d/xxx:系统升级到Cento7后,虽然之前的启动脚本也可以使用,但一直没有使用systemctl 的自定义脚本. ...

  7. CentOS7使用systemctl添加自定义服务

    一.简介 Centos7开机第一个程序从init完全换成了systemd这种启动方式,同centos 5 6已经是实质差别.systemd是靠管理unit的方式来控制开机服务,开机级别等功能. 在/u ...

  8. CentOS7 上systemctl

    CentOS 上systemctl 的用法 [日期:--] 来源:Linux社区 作者:Linux [字体:大 中 小] 我们对service和chkconfig两个命令都不陌生,systemctl ...

  9. Linux学习笔记之Centos7 自定义systemctl服务脚本

    0x00 概述 之前工作环境一直使用Centos6版本,脚本一直在使用/etc/init.d/xxx:系统升级到Cento7后,虽然之前的启动脚本也可以使用,但一直没有使用systemctl 的自定义 ...

  10. 对比centos7的systemctl和其他service+chkconfig

    syetemctl就是service和chkconfig这两个命令的整合,在CentOS 7就开始被使用了.systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig ...

随机推荐

  1. Spring整合mybatis使用xml配置事务

    自己准备开始教授Java相关的技术,Spring框架是必须让学生学习的框架之一.里面有一个事务相关的 配置,以前刚学习Spring框架的时候有接触过,只是过了很多年,很多东西都已经淡忘.再来回忆一下 ...

  2. AOP+自定义注解实现权限校验-2022新项目

    一.业务场景 当前本人参与开发的是一个业务中台系统,所谓的中台简单的理解就是把相同的功能给抽取出来.比如系统A.B.C.D都需要进行用户登录操作,那么可以把用户信息管理这一块抽取出来为一个独立的系统E ...

  3. 在linux上安装redis并设置权限

    redis是使用 c 开发,启动文件是二进制的看不到什么有用的信息,安装最新版本可以在https://redis.io/download 官网上查看,安装非常简单: mkdir /usr/redis, ...

  4. git 撤销本地 git提交的commit记录 (git reset --hard ID)

    git 撤销本地 git提交的commit记录 (git reset --hard ID) ID的获取方法 这个id,就是你要退回的那个id,我这里截图的时候已经回退了,正常是你提错了的下面那个git ...

  5. git reset --soft HEAD^^ 项目提交代码冲突 提交当前merge后,别提交远程,用命令回滚2次,到未提交的版本,再拉取同事代码,冲突解决

    git reset --soft HEAD^^ 项目提交代码冲突 提交当前merge后,别提交远程,用命令回滚2次,到未提交的版本,再拉取同事代码,冲突解决 当多人写一个项目,最好还是关掉vscode ...

  6. leetcode算法之Valid Anagram

    原文算法说明如下: Given two strings s and t, write a function to determine if t is an anagram of s. For exam ...

  7. display标签交替显示不同行颜色

    问题 想要一个更加简单的支持列表数据分页和排序的方法. 解决方案 使用Display标签库和JSP标签库. 例4.13介绍了一个JSP页,它使用4.5节中的数据模型显示美国总统列表.这个JSP页面使用 ...

  8. 22_播放器之使用SDL显示YUV视频

    简介 使用SDL实现简单的YUV播放器. 这里还需要使用到像素格式和计算图片大小,这两个我们直接使用ffmpeg来实现,因此需要使用ffmpeg的libavutil/avutil.h和libavuti ...

  9. 关于全景(360)图片拼接的方法(Opencv3.0 Stitcher)

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  10. 安装centos系统必做操作

    关闭SELinux sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config 在Linux系统中临时关闭SELinux(Security-E ...