Liniux systemctl

systemctl 命令

systemctl命令控制:启动、停止、开机自启能够被syatemctl管理的软件,一般也称之为服务

语法:systemctl start | stop| status |enable |disable 服务名

(启动/停止/查看状态/开启开机自启/关闭开机自启)

NetworkManager 主网络服务

network 副网络

firewalld 防火墙

sshd,ssh服务(Xshell远程都玩Linux使用的就是这个服务)

# systemctl list-units --type=service --state=running 查看当前正在运行中的服务
root@hcss-ecs-c2b8:~# systemctl list-units --type=service --state=running
  UNIT                             LOAD   ACTIVE SUB     DESCRIPTION                                   
  atd.service                      loaded active running Deferred execution scheduler
  chrony.service                   loaded active running chrony, an NTP client/server
  cloudResetPwdUpdateAgent.service loaded active running cloudResetPwdUpdateAgent
  containerd.service               loaded active running containerd container runtime
  cron.service                     loaded active running Regular background program processing daemon
  dbus.service                     loaded active running D-Bus System Message Bus
  docker.service                   loaded active running Docker Application Container Engine
  fwupd.service                    loaded active running Firmware update daemon
  getty@tty1.service               loaded active running Getty on tty1
  irqbalance.service               loaded active running irqbalance daemon
  ModemManager.service             loaded active running Modem Manager
  multipathd.service               loaded active running Device-Mapper Multipath Device Controller
  mysqld.service                   loaded active running LSB: start and stop MySQL
  NetworkManager.service           loaded active running Network Manager
  nginx.service                    loaded active running nginx - high performance web server
  packagekit.service               loaded active running PackageKit Daemon
  polkit.service                   loaded active running Authorization Manager
  rsyslog.service                  loaded active running System Logging Service
  serial-getty@ttyS0.service       loaded active running Serial Getty on ttyS0
  snapd.service                    loaded active running Snap Daemon
  ssh.service                      loaded active running OpenBSD Secure Shell server
  systemd-journald.service         loaded active running Journal Service
  systemd-logind.service           loaded active running User Login Management
  systemd-resolved.service         loaded active running Network Name Resolution
  systemd-udevd.service            loaded active running Rule-based Manager for Device Events and Files
  udisks2.service                  loaded active running Disk Manager
  unattended-upgrades.service      loaded active running Unattended Upgrades Shutdown
  uniagent.service                 loaded active running Unified Agent
  upower.service                   loaded active running Daemon for power management
  user@0.service                   loaded active running User Manager for UID 0
  uuidd.service                    loaded active running Daemon for generating UUIDs
  wpa_supplicant.service           loaded active running WPA supplicant

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
32 loaded units listed.

这里我在服务器上安装了一个mysql数据,mysql数据库安装完成后他会自己注册为系统的一个服务,这样我们能直接通过systemctl命令对mysql的状态进行管理

# 先查看服务列表是否有mysql的相关服务
root@hcss-ecs-c2b8:~# systemctl list-units --type=service | grep mysql
  mysqld.service                     loaded active running LSB: start and stop MySQL
# systemctl status mysql 查看mysql的状态
root@hcss-ecs-c2b8:~# systemctl status mysql
● mysqld.service - LSB: start and stop MySQL
     Loaded: loaded (/etc/init.d/mysqld; generated)
     Active: active (running) since Sat 2024-09-28 10:12:41 CST; 1min 53s ago
       Docs: man:systemd-sysv-generator(8)
    Process: 96068 ExecStart=/etc/init.d/mysqld start (code=exited, status=0/SUCCESS)
      Tasks: 34 (limit: 2006)
     Memory: 362.1M
        CPU: 1.198s
     CGroup: /system.slice/mysqld.service
             ├─96081 /bin/sh /www/server/mysql/bin/mysqld_safe --datadir=/www/server/data --pid-file=/www/server/data/hcss-ecs-c2b8.pid
             └─96706 /www/server/mysql/bin/mysqld --basedir=/www/server/mysql --datadir=/www/server/data --plugin-dir=/www/server/mysql/lib/plugin ->

Sep 28 10:12:40 hcss-ecs-c2b8 systemd[1]: Starting LSB: start and stop MySQL...
Sep 28 10:12:41 hcss-ecs-c2b8 mysqld[96068]: Starting MySQL. *
Sep 28 10:12:41 hcss-ecs-c2b8 systemd[1]: Started LSB: start and stop MySQL.
lines 1-15/15 (END)

现在我们去吧运行中的mysqld服务停止掉,并查看其状态

root@hcss-ecs-c2b8:~# systemctl stop mysqld
root@hcss-ecs-c2b8:~# systemctl status mysqld
○ mysqld.service - LSB: start and stop MySQL
     Loaded: loaded (/etc/init.d/mysqld; generated)
     Active: inactive (dead) #这由原来的running 变成了 dead
       Docs: man:systemd-sysv-generator(8)

对于这种服务,我们希望他永远不要停止,就算是服务器重启,我们也希望他能在服务器重启后也能自己启动,这是我们就需要用enable和disable这个命令来关闭和开启服务的开机自启

root@hcss-ecs-c2b8:~# systemctl enable mysqld
# 这里提示的很清楚,我们这个mysqld不是本地服务所以需要使用重定向到systemd-sysv-install这里去开启
# 我们直接复制下面这个executing:后面的命令就好了,本地服务就能直接成功,disable命令也是这样的
mysqld.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable mysqld
root@hcss-ecs-c2b8:~# /lib/systemd/systemd-sysv-install enable mysqld
root@hcss-ecs-c2b8:~# systemctl disable mysqld
mysqld.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable mysqld
root@hcss-ecs-c2b8:~# /lib/systemd/systemd-sysv-install disable mysqld

这里我们重启一下无服务器,立马查看服务状态就能知道他是否开机自启

目前介绍了本地服务和第三方服务的状态和开机自启管理

这里我们来实现以下,如何让我们自己编写的脚本开机自启

首先我们编写一个测试脚本

[Unit]
 
Description=Run a Custom Script at Startup


[Service]

ExecStart=/var/test/auto_start.sh
ExecStop=/var/test/auto_start.sh

[Install]

WantedBy=multi-user.target                        

在上面的配置文件中,为了演示起见,我将一些本测试脚本不需要但是比较重要的配置项也写了出来,其实如果不需要可以删除,但是[Unit]/[Service]/[Install]这三个标签需要保留。
我们来一个个简单介绍一下配置项:

Description:运行软件描述
Documentation:软件的文档
After:因为软件的启动通常依赖于其他软件,这里是指定在哪个服务被启动之后再启动,设置优先级
Wants:弱依赖于某个服务,目标服务的运行状态可以影响到本软件但不会决定本软件运行状态
Requires:强依赖某个服务,目标服务的状态可以决定本软件运行。
ExecStart:执行命令
ExecStop:停止执行命令
ExecReload:重启时的命令
Type:软件运行方式,默认为simple
WantedBy:这里相当于设置软件,选择运行在linux的哪个运行级别,只是在systemd中不在有运行级别概念,但是这里权当这么理解。 

如果有多项,用逗号作为分隔。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值