CentOS7 的开机自启动systemctl

说明

centos7自启项已不用chkconfig改为:

systemctl list-unit-files

启停

systemctl enable redis
systemctl disable redis


分类

根据后缀名识别类型
    1. .service - 定义系统服务的启动
    2. .target定义了系统启动级别的标签, systemd没有运行级别的概念, 创建标签是为了兼容老版本
    3. .socket定义了进程通用的套接字, 套接字和进程是分离的
    4. .device 定义了系统启动时内核识别的文件, systemd提供了设备的管理功能, /dev下的设备由/etc/udev下的配置文件和.device共同定制
    5. .mount 定义系统的fs挂载点
    6. .snapshop 系统快照
    7. .swap 用于识别swap设备
    8. .automount 文件系统的自动挂载点
    9. .path 用于定义fs中的一个文件或目录, 常用与fs发生变化时, 延迟激活服务


脚本

通常由3本分组成

[Unit]
[unit的类型: service target socket]
[install]

示例:

[Unit] 
   
Description=mongodb  
After=network.target remote-fs.target nss-lookup.target 
   
[Service] 
Type=forking 
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID 
ExecStop=/usr/local/mongodb/bin/mongod --shutdown -f /usr/local/mongodb/mongodb.conf
PrivateTmp=true 
     
[Install] 
WantedBy=multi-user.target 

附录:

[Unit]区块字段描述

  • Description:简短描述
  • Documentation:文档地址
  • Requires:当前 Unit 依赖的其他 Unit,如果它们没有运行,当前 Unit 会启动失败
  • Wants:与当前 Unit 配合的其他 Unit,如果它们没有运行,当前 Unit 不会启动失败
  • BindsTo:与Requires类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行
  • Before:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动
  • After:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动
  • Conflicts:这里指定的 Unit 不能与当前 Unit 同时运行
  • Condition...:当前 Unit 运行必须满足的条件,否则不会运行
  • Assert...:当前 Unit 运行必须满足的条件,否则会报启动失败

 [Service]区块字段描述

  • Type:定义启动时的进程行为。它有以下几种值。
  • Type=simple:默认值,执行ExecStart指定的命令,启动主进程
  • Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
  • Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
  • Type=dbus:当前服务通过D-Bus启动
  • Type=notify:当前服务启动完毕,会通知Systemd,再继续往下执行
  • Type=idle:若有其他任务执行完毕,当前服务才会运行
  • ExecStart:启动当前服务的命令
  • ExecStartPre:启动当前服务之前执行的命令
  • ExecStartPost:启动当前服务之后执行的命令
  • ExecReload:重启当前服务时执行的命令
  • ExecStop:停止当前服务时执行的命令
  • ExecStopPost:停止当其服务之后执行的命令
  • RestartSec:自动重启当前服务间隔的秒数
  • Restart:定义何种情况 Systemd 会自动重启当前服务 
    no(默认值): # 退出后无操作

    on-success:  # 只有正常退出时(退出状态码为0),才会重启

    on-failure:  # 非正常退出时,重启,包括被信号终止和超时等

    on-abnormal: # 只有被信号终止或超时,才会重启

    on-abort:    # 只有在收到没有捕捉到的信号终止时,才会重启

    on-watchdog: # 超时退出时,才会重启

    always:      # 不管什么退出原因,都会重启(除了systemctl stop)

    # 对于守护进程,推荐用on-failure
  • KillMode的类型:
    control-group(默认):# 当前控制组里的所有子进程,都会被杀掉

    process: # 只杀主进程

    mixed:   # 主进程将收到SIGTERM信号,子进程收到SIGKILL信号

    none:    # 没有进程会被杀掉,只是执行服务的stop命令
  • TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
  • Environment:指定环境变量

[Install]字段描述

  • WantedBy:它的值是一个或多个 Target,当前 Unit 激活时(enable)符号链接会放入/etc/systemd/system目录下面以 Target 名 + .wants后缀构成的子目录中
  •     multi-user.target: # 表示多用户命令行状态,这个设置很重要
  •     graphical.target:  # 表示图形用户状体,它依赖于multi-user.target
  • RequiredBy:它的值是一个或多个 Target,当前 Unit 激活时,符号链接会放入/etc/systemd/system目录下面以 Target 名 + .required后缀构成的子目录中
  • Alias:当前 Unit 可用于启动的别名
  • Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit

命令

基础操作:systemctl start|stop|restart|status name.service

条件式重启:

即服务之前是启动的则进行重启,如果服务没有启动则不进行操作
systemctl try-restart name.service

重载或重启:

首先进行重载,如果重载不成功则进行重启
systemctl reload-or-restart name.service

重载或条件式重启:

扫描二维码关注公众号,回复: 9099151 查看本文章

systemctl reload-or-try-restart name.service

设置服务是否可以被用户设置开机启动状态

systemctl unmask name.service  取消禁止
systemctl mask name.service 禁止

查看服务的当前激活状态:

服务已经启动命令的状态返回值为0 命令未启动命令的状态返回值为非0值
systemctl is-active name.service

查看所有已经激活的服务:

-t 指定显示的unit类型。
-a 或 --all 显示更加详细的信息列表。

systemctl list-units
systemctl list-units -t service
systemctl list-units -t service -a

查看所有服务:

systemctl list-units  -a
1
查看所有服务状态:

systemctl list-unit-files
-a 或--all : 查看所有服务的状态 
-t 或--type :指定需要查看的unit类型

loaded :配置文件已经加载,载入内存
active(running):一次或多次持续处理的运行
active(exited):成功完成一次性的配置
active(waiting):运行中,等待一个事件
inactive:不运行
enabled:开机启动
disabled:开机不启动
static:开机不启动,但可被另一个启用的服务激活

用来列出该服务在哪些运行级别下启用和禁用

ls /etc/systemd/system/*.wants/sshd.service
systemctl list-unit-files --type target --all


设置服务开机不启动:

systemctl disable 服务名称unit

查看服务是否开机自启:

systemctl is-enabled name.service

察看服务的依赖关系:

systemctl list-dependencies name.service
systemctl list-depebdencies


重载服务:

systemctl daemon-reload

杀掉进程:

systemctl kill 进程名
 

发布了144 篇原创文章 · 获赞 53 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/yanxilou/article/details/102491560