!!!在开启一天的学习的时候,先做好快照,过程中如果出现意外的报错,解决不了的就立即恢复快照,作为初学者,省时省力,不要过于纠结哪里错了,浪费时间!!!
windows 开机的时候,有些程序自动启动了。
命令行运行services.msc查看相关服务的启动情况
1.systemd 介绍
1.1 基本概念
CentOS 7 使用 Systemd 引导系统启动,速度最快,所有进程无论有无依赖关系则都是并行启动(很多时候进程没有真正启动而是只有一个信号或者说是标记而已,在真正利用的时候才会真正启动)。Systemd为了解决上文的问题而诞生。它的目标是,为系统的启动和管理提供一套完整的解决方案。
系统引导程序:
- 用户空间,systemd,pid为1
- 内核空间,kthreadd,pid为2
服务:从业务角度来称呼,例如 web 服务,数据库服务。
守护进程(daemon):web 服务器对外提供 web 服务,由 web 相关的进程提供支持。
以web服务为例:
服务:web服务
守护进程:httpd
万和提供培训服务
- 服务:培训云计算
- 守护进程:课程顾问、代课老师、就业老师等。
例如:
# 安装软件包 [root@centos7 ~ 11:12:41]# yum install -y httpd # 启动服务 [root@centos7 ~ 11:12:59]# systemctl start httpd # 查看进程 [root@centos7 ~ 11:13:31]# ps -C httpd f PID TTY STAT TIME COMMAND 4706 ? Ss 0:00 /usr/sbin/httpd -DFOREGROUND 4709 ? S 0:00 \_ /usr/sbin/httpd -DFOREGROUND 4710 ? S 0:00 \_ /usr/sbin/httpd -DFOREGROUND 4711 ? S 0:00 \_ /usr/sbin/httpd -DFOREGROUND 4712 ? S 0:00 \_ /usr/sbin/httpd -DFOREGROUND 4713 ? S 0:00 \_ /usr/sbin/httpd -DFOREGROUNDhttpd 服务对应的守护进程是4706、4709...4713
1.2 systemd 架构
守护进程:systemd,由守护进程干活。
工具:systemctl,提供给用户,用来跟机器交互的工具。
1.3 unit 类型
systemctl 命令用于管理不同类型的系统对象,这些对象称之为units。
- Service unit:用于定义系统服务,文件扩展名为.service,例如httpd.service
- Socket unit:用于标识进程间通信用的 socket文件,文件扩展名为.socket
- Target unit:用于模拟实现“运行级别”,文件扩展名为.target
- Timer unit:用于管理计划任务,文件扩展名为.timer
- Device unit:用于定义内核识别的设备,文件扩展名为.device
- Mount unit:用于定义文件系统挂载点,文件扩展名为.mount
- Snapshot unit:管理系统快照,文件扩展名为.snapshot
- Swap unit:用于标识swap设备,文件扩展名为.swap
- Automount unit:文件系统的自动挂载点,文件扩展名为.automount
- Path unit:用于根据文件系统上特定对象的变化来启动其他服务,文件扩展名为.path
- Slice unit:用于资源管理,文件扩展名为.slice
1.4 查看 unit 列表信息
# 列出状态为loaded units [root@centos7 ~ 11:32:38]# systemctl list-unitssystemctl list-units命令输出说明:
- UNIT:服务单元名称。
- LOAD:systemd是否正确解析了单元的配置并将该单元加载到内存中。
- ACTIVE:单元的高级别激活状态。此信息表明单元是否已成功启动。
- SUB:单元的低级别激活状态。此信息指示有关该单元的更多详细信息。信息视单元类型、 状态以及单元的执行方式而异。
- DESCRIPTION:单元的简短描述。
# -t选项查看特定类型unit 清单 [root@centos7 ~ 11:34:55]# systemctl list-units -t timer UNIT LOAD ACTIVE SUB DESCRIPTION systemd-tmpfiles-clean.timer loaded active waiting Daily Cleanup of Temporary Direc unbound-anchor.timer loaded active waiting daily update of the root trust a 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. 2 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'. # 列出类型为service,状态为active和inactive unit [root@centos7 ~ 11:35:58]# systemctl list-units --type service --all UNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hoo abrt-oops.service loaded active running ABRT kernel log watcher abrt-vmcore.service loaded inactive dead Harvest vmcores for ABRT abrt-xorg.service loaded active running ABRT Xorg log watcher abrtd.service loaded active running ABRT Automated Bug Report accounts-daemon.service loaded active running Accounts Service alsa-restore.service loaded inactive dead Save/Restore Sound Card S alsa-state.service loaded active running Manage Sound Card State ( ● apparmor.service not-found inactive dead apparmor.service atd.service loaded active running Job spooling tools ·········· # 列出系统中所有unit,包括未loaded的unit [root@centos7 ~ 11:35:03]# systemctl list-unit-files UNIT FILE STATE proc-sys-fs-binfmt_misc.automount static dev-hugepages.mount static dev-mqueue.mount static proc-fs-nfsd.mount static proc-sys-fs-binfmt_misc.mount static run-vmblock\x2dfuse.mount disabled sys-fs-fuse-connections.mount static sys-kernel-config.mount static sys-kernel-debug.mount static tmp.mount disabled var-lib-nfs-rpc_pipefs.mount static brandbot.path disabled cups.path enabled systemd-ask-password-console.path static ············· # 查看失败的服务 [root@centos7 ~ 11:37:34]# systemctl --failed --type service 0 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'.1.5 查看单个 unit 信息
[fengkai@centos7 ~ 13:35:31]$ systemctl status sshd.service ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 三 2026-07-22 08:31:31 CST; 5h 7min ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 1205 (sshd) Tasks: 1 CGroup: /system.slice/sshd.service └─1205 /usr/sbin/sshd -D 7月 22 08:31:31 centos7.cloud systemd[1]: Starting OpenSSH server daemon... 7月 22 08:31:31 centos7.cloud sshd[1205]: Server listening on 0.0.0.0 port 22. 7月 22 08:31:31 centos7.cloud sshd[1205]: Server listening on :: port 22. 7月 22 08:31:31 centos7.cloud systemd[1]: Started OpenSSH server daemon. 7月 22 08:34:09 centos7.cloud sshd[2194]: Accepted password for fengkai from 10.1.8.1 port 51023 ssh2 7月 22 09:07:30 centos7.cloud sshd[2607]: Accepted password for fengkai from 10.1.8.1 port 58696 ssh2 7月 22 09:09:12 centos7.cloud sshd[2664]: Accepted password for fengkai from 10.1.8.1 port 58848 ssh2 7月 22 09:13:03 centos7.cloud sshd[2785]: Accepted password for fengkai from 10.1.8.1 port 59005 ssh2 7月 22 09:57:31 centos7.cloud sshd[3320]: Accepted password for fengkai from 10.1.8.1 port 60996 ssh2| 关键字 | 概述 |
|---|---|
| loaded | 单元配置文件已处理 |
| active(running) | 正在运行 |
| active(exited) | 已成功完成一次性配置 |
| active(waiting) | 运行中,正在等待事件 |
| inactive | 不再运行 |
| enabled | 系统引导时启动 |
| disabled | 系统引导时不启动 |
| static | 无法启动,依赖其他单元启动 |
2.控制系统服务
| 命令 | 任务 |
|---|---|
| systemctl statusUNIT | 查看单元状态的详细信息。 |
| systemctl stopUNIT | 在运行中的系统上停止一项服务。 |
| systemctl startUNIT | 在运行中的系统上启动一项服务。 |
| systemctl restartUNIT | 在运行中的系统上重新启动一项服务。 |
| systemctl reloadUNIT | 重新加载运行中服务的配置文件。 |
| systemctl maskUNIT | 禁用服务,使其无法手动启动或在系统引导时启动。 |
| systemctl unmaskUNIT | 使屏蔽的服务变为可用。 |
| systemctl enableUNIT | 将服务配置为在系统引导时启动。使用--now选项也会启动该服务。 |
| systemctl disableUNIT | 禁止服务在系统引导时启动。使用--now选项也会停止该服务。 |
# 停止服务 [root@centos7 ~ 13:36:48]# systemctl stop sshd.service [root@centos7 ~ 13:56:23]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: inactive (dead) since 三 2026-07-22 13:56:23 CST; 43s ago Docs: man:sshd(8) man:sshd_config(5) Process: 1205 ExecStart=/usr/sbin/sshd -D $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 1205 (code=exited, status=0/SUCCESS) 7月 22 08:31:31 centos7.cloud sshd[1205]: Server listening on :: port 22. 7月 22 08:31:31 centos7.cloud systemd[1]: Started OpenSSH server daemon. 7月 22 08:34:09 centos7.cloud sshd[2194]: Accepted password for fengkai from 10.1.8.1 port 51023 ssh2 7月 22 09:07:30 centos7.cloud sshd[2607]: Accepted password for fengkai from 10.1.8.1 port 58696 ssh2 7月 22 09:09:12 centos7.cloud sshd[2664]: Accepted password for fengkai from 10.1.8.1 port 58848 ssh2 7月 22 09:13:03 centos7.cloud sshd[2785]: Accepted password for fengkai from 10.1.8.1 port 59005 ssh2 7月 22 09:57:31 centos7.cloud sshd[3320]: Accepted password for fengkai from 10.1.8.1 port 60996 ssh2 7月 22 13:56:23 centos7.cloud sshd[1205]: Received signal 15; terminating. 7月 22 13:56:23 centos7.cloud systemd[1]: Stopping OpenSSH server daemon... 7月 22 13:56:23 centos7.cloud systemd[1]: Stopped OpenSSH server daemon. # 客户端连接测试 [root@centos7 ~ 13:57:07]# ssh fengkai@localhost ssh: connect to host localhost port 22: Connection refused # 启动服务 [root@centos7 ~ 13:57:48]# systemctl start sshd.service # 客户端连接测试 [root@centos7 ~ 13:58:20]# ssh fengkai@localhost The authenticity of host 'localhost (::1)' can't be established. ECDSA key fingerprint is SHA256:5KkrnsINIP6anjuKXNFLmX2lITpgeyRlv6uEWtvRkgU. ECDSA key fingerprint is MD5:70:76:1e:4b:c6:85:44:ff:03:02:07:29:53:d9:1a:f8. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts. fengkai@localhost's password: Last login: Wed Jul 22 09:57:32 2026 from 10.1.8.1 [fengkai@centos7 ~ 13:58:33]$ # 重启服务,相当于stop再start,PID由6727变为6799 [root@centos7 ~ 14:00:32]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 三 2026-07-22 14:00:22 CST; 17s ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 6727 (sshd) Tasks: 1 CGroup: /system.slice/sshd.service └─6727 /usr/sbin/sshd -D 7月 22 14:00:22 centos7.cloud systemd[1]: Stopped OpenSSH server daemon. 7月 22 14:00:22 centos7.cloud systemd[1]: Starting OpenSSH server daemon... 7月 22 14:00:22 centos7.cloud sshd[6727]: Server listening on 0.0.0.0 port 22. 7月 22 14:00:22 centos7.cloud sshd[6727]: Server listening on :: port 22. 7月 22 14:00:22 centos7.cloud systemd[1]: Started OpenSSH server daemon. [root@centos7 ~ 14:00:39]# systemctl restart sshd [root@centos7 ~ 14:01:23]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 三 2026-07-22 14:01:23 CST; 3s ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 6799 (sshd) Tasks: 1 CGroup: /system.slice/sshd.service └─6799 /usr/sbin/sshd -D 7月 22 14:01:23 centos7.cloud systemd[1]: Stopped OpenSSH server daemon. 7月 22 14:01:23 centos7.cloud systemd[1]: Starting OpenSSH server daemon... 7月 22 14:01:23 centos7.cloud sshd[6799]: Server listening on 0.0.0.0 port 22. 7月 22 14:01:23 centos7.cloud sshd[6799]: Server listening on :: port 22. 7月 22 14:01:23 centos7.cloud systemd[1]: Started OpenSSH server daemon. # 一般用于配置文件变动后,重新加载,PID还是6799不变 [root@centos7 ~ 14:01:26]# systemctl reload sshd.service [root@centos7 ~ 14:03:05]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 三 2026-07-22 14:01:23 CST; 1min 44s ago Docs: man:sshd(8) man:sshd_config(5) Process: 6836 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS) Main PID: 6799 (sshd) Tasks: 1 CGroup: /system.slice/sshd.service └─6799 /usr/sbin/sshd -D 7月 22 14:01:23 centos7.cloud systemd[1]: Stopped OpenSSH server daemon. 7月 22 14:01:23 centos7.cloud systemd[1]: Starting OpenSSH server daemon... 7月 22 14:01:23 centos7.cloud sshd[6799]: Server listening on 0.0.0.0 port 22. 7月 22 14:01:23 centos7.cloud sshd[6799]: Server listening on :: port 22. 7月 22 14:01:23 centos7.cloud systemd[1]: Started OpenSSH server daemon. 7月 22 14:03:05 centos7.cloud systemd[1]: Reloading OpenSSH server daemon. 7月 22 14:03:05 centos7.cloud sshd[6799]: Received SIGHUP; restarting. 7月 22 14:03:05 centos7.cloud systemd[1]: Reloaded OpenSSH server daemon. 7月 22 14:03:05 centos7.cloud sshd[6799]: Server listening on 0.0.0.0 port 22. 7月 22 14:03:05 centos7.cloud sshd[6799]: Server listening on :: port 22. # 重新加载服务,服务对应的主进程不会重启,只会重新加载一次配置文件。 # 禁止服务开机自启 [root@centos7 ~ 14:03:07]# systemctl disable sshd.service Removed symlink /etc/systemd/system/multi-user.target.wants/sshd.service. [root@centos7 ~ 14:04:10]# systemctl is-enabled sshd disabled [root@centos7 ~ 14:04:25]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled) Active: active (running) since 三 2026-07-22 14:01:23 CST; 3min 17s ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 6799 (sshd) CGroup: /system.slice/sshd.service └─6799 /usr/sbin/sshd -D 7月 22 14:01:23 centos7.cloud systemd[1]: Stopped OpenSSH server daemon. 7月 22 14:01:23 centos7.cloud systemd[1]: Starting OpenSSH server daemon... 7月 22 14:01:23 centos7.cloud sshd[6799]: Server listening on 0.0.0.0 port 22. 7月 22 14:01:23 centos7.cloud sshd[6799]: Server listening on :: port 22. 7月 22 14:01:23 centos7.cloud systemd[1]: Started OpenSSH server daemon. 7月 22 14:03:05 centos7.cloud systemd[1]: Reloading OpenSSH server daemon. 7月 22 14:03:05 centos7.cloud sshd[6799]: Received SIGHUP; restarting. 7月 22 14:03:05 centos7.cloud systemd[1]: Reloaded OpenSSH server daemon. 7月 22 14:03:05 centos7.cloud sshd[6799]: Server listening on 0.0.0.0 port 22. 7月 22 14:03:05 centos7.cloud sshd[6799]: Server listening on :: port 22. # 重启系统验证 [root@centos7 ~ 14:04:03]# reboot # 通过Xshell连接不上 Connecting to 10.1.8.10:22... Could not connect to '10.1.8.10' (port 22): Connection failed. Type `help' to learn how to use Xshell prompt. # 设置服务开机自启,通过VMware直接连接 [root@centos7 ~ 14:07:51]# systemctl enable sshd.service Created symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service. [root@centos7 ~ 14:08:03]# systemctl is-enabled sshd enabled # 重启系统验证 [root@centos7 ~ 14:08:10]# reboot # 通通过Xshell可以连接 Connecting to 10.1.8.10:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Last login: Wed Jul 22 14:07:04 2026 [fengkai@centos7 ~ 14:09:46]$ # 禁用服务:服务被禁用后,将无法start,因为服务的配置文件指向/dev/null [root@centos7 ~ 14:10:32]# systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since 三 2026-07-22 14:09:03 CST; 1min 43s ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 1171 (sshd) Tasks: 1 CGroup: /system.slice/sshd.service └─1171 /usr/sbin/sshd -D 7月 22 14:09:03 centos7.cloud systemd[1]: Starting OpenSSH server daemon... 7月 22 14:09:03 centos7.cloud sshd[1171]: Server listening on 0.0.0.0 port 22. 7月 22 14:09:03 centos7.cloud sshd[1171]: Server listening on :: port 22. 7月 22 14:09:03 centos7.cloud systemd[1]: Started OpenSSH server daemon. 7月 22 14:09:45 centos7.cloud sshd[2126]: Accepted password for fengkai from 10.1.8.1 port 56569 ssh2 [root@centos7 ~ 14:10:47]# systemctl mask sshd.service Created symlink from /etc/systemd/system/sshd.service to /dev/null. [root@centos7 ~ 14:11:02]# systemctl status sshd ● sshd.service Loaded: masked (/dev/null; bad) Active: active (running) since 三 2026-07-22 14:09:03 CST; 2min 0s ago Main PID: 1171 (sshd) CGroup: /system.slice/sshd.service └─1171 /usr/sbin/sshd -D 7月 22 14:09:03 centos7.cloud systemd[1]: Starting OpenSSH server daemon... 7月 22 14:09:03 centos7.cloud sshd[1171]: Server listening on 0.0.0.0 port 22. 7月 22 14:09:03 centos7.cloud sshd[1171]: Server listening on :: port 22. 7月 22 14:09:03 centos7.cloud systemd[1]: Started OpenSSH server daemon. 7月 22 14:09:45 centos7.cloud sshd[2126]: Accepted password for fengkai from 10.1.8.1 port 56569 ssh2 7月 22 14:11:02 centos7.cloud systemd[1]: Current command vanished from the unit file, execution of the com...umed. Hint: Some lines were ellipsized, use -l to show in full. # 取消禁用 [root@centos7 ~ 14:14:18]# systemctl unmask sshd.service3.unit 配置文件
unit 配置文件存放在多个位置:
- /etc /systemd/system/unit.service,优先生效。一般是管理员自定义的配置。
- /usr/lib /systemd/system/unit.service,其次生效。软件包自带的默认配置。
3.1 配置文件说明
示例单元文件/usr/lib/systemd/system/sshd.service说明
# 标识该部分为 Unit 配置,用于描述服务的基本信息、依赖关系等。 [Unit] # 服务的描述信息,说明这是 "OpenSSH 服务器守护进程",便于管理员识别服务用途。 Description=OpenSSH server daemon # 指定服务的文档路径,这里指向 sshd 命令的手册页(man 8 sshd)和配置文件的手册页(man 5 sshd_config),方便用户查阅帮助。 Documentation=man:sshd(8) man:sshd_config(5) # 定义服务的启动顺序:sshd 服务必须在 network.target(网络服务就绪)和 sshd-keygen.service(SSH 密钥生成服务)之后启动,确保依赖的资源已准备好。 After=network.target sshd-keygen.service # 表示 sshd 服务 "希望" sshd-keygen.service 运行(但不是强制依赖)。如果 sshd-keygen.service 启动失败,sshd 仍会尝试启动(通常用于生成初始 SSH 密钥,若密钥已存在则不影响)。 Wants=sshd-keygen.service # 标识该部分为 Service 配置,用于定义服务的启动方式、执行命令、重启策略等。 [Service] # 定义服务的类型为 notify:表示服务启动后会主动通知 systemd 自己已就绪(通过 sd_notify() 函数),systemd 会等待这个通知后再继续后续流程,确保服务真正可用。 Type=notify # 指定环境变量文件的路径,/etc/sysconfig/sshd 中通常定义 OPTIONS 等变量(如额外的 sshd 启动参数),这些变量会被后续的 ExecStart 引用。 EnvironmentFile=/etc/sysconfig/sshd # 服务启动时执行的命令: ExecStart=/usr/sbin/sshd -D $OPTIONS # /usr/sbin/sshd:sshd 守护进程的可执行文件路径。 # -D:表示 sshd 以非守护进程模式运行(前台运行),因为 systemd 通常管理前台进程,便于监控。 # $OPTIONS:引用 EnvironmentFile 中定义的额外参数(如 -p 2222 指定端口)。 # 服务重载配置时执行的命令: ExecReload=/bin/kill -HUP $MAINPID # kill -HUP 发送 SIGHUP 信号给 sshd 主进程,使其重新加载配置文件(无需重启服务)。 # $MAINPID 是 systemd 自动维护的服务主进程 ID。 # 定义服务停止时的杀死模式:process 表示只杀死服务的主进程(sshd 主进程),其子进程(如已建立的 SSH 连接)会被保留(避免强制中断现有连接)。 KillMode=process # 定义服务的重启策略:当服务因非正常退出(如崩溃、信号终止)时,systemd 会自动重启服务;正常退出(如主动停止)则不重启。 Restart=on-failure # 服务重启前的等待时间,这里设置为 42 秒,避免频繁重启导致资源耗尽。 RestartSec=42s # 标识该部分为 Install 配置,用于定义服务如何被 "启用"(即系统启动时自动运行)。 [Install] # 表示当系统启动到 multi-user.target(多用户命令行模式,非图形界面)时,该服务会被自动启动。这是服务器的默认运行级别,确保 SSH 服务在系统启动后可用。 WantedBy=multi-user.target3.2 开发一个 study 服务
3.2.1 开发 studyd 服务主程序 study
脚本说明:这是一个无限循环的脚本,每 5 秒会向/var/log/study.log文件中追加一行包含当前时间的日志,内容为[时间]: I'M studying [ Linux ]。
[root@centos7 ~ 14:27:30]# vim /usr/local/bin/study#!/bin/bash # 第一行内容是脚本的 "解释器声明"(shebang),指定该脚本使用 /bin/bash 作为解释器执行。系统会根据这一行找到对应的 shell 程序来解析后续命令。 # 启动一个无限循环:while 是循环关键字,true 是一个永远为真的条件,因此这个循环会一直执行下去,直到被外部终止(如 Ctrl+C)。 while true # 循环体的开始标记,do 和后面的 done 之间的内容是循环中重复执行的命令。 do # 执行 date 命令(获取当前系统时间),并通过 $(...) 捕获其输出,将结果赋值给变量 DATE。 DATE=$(date) # echo 命令输出字符串,其中 $DATE 会被替换为变量的值 # >> 是追加重定向符号,将输出内容追加到 /var/log/study.log 文件中 # 最终输出内容类似 Fri Oct 31 10:00:00 CST 2025: I'M studying [ Linux ]。 echo "$DATE: I'M studying [ Linux ]" >> /var/log/study.log # 让脚本暂停执行 5 秒(sleep 命令用于延迟,单位默认为秒),避免循环执行过快。 sleep 5 # 循环体的结束标记,与前面的 while 和 do 配合,标志着一次循环的结束。 done[root@centos7 ~ 16:24:17]# chmod +x /usr/local/bin/study [root@centos7 ~ 14:56:07]# ls -l /usr/local/bin/study -rwxr-xr-x. 1 root root 1185 7月 22 14:55 /usr/local/bin/study3.2.2 创建 studyd 服务单元文件
# 参考 sshd.service [root@centos7 ~ 14:56:07]# cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/studyd.service [root@centos7 ~ 14:57:38]# vim /etc/systemd/system/studyd.service[Unit] Description=study server daemon [Service] ExecStart=/usr/local/bin/study [Install] WantedBy=multi-user.target# 通知 systemd 读取 unit 变化 [root@centos7 ~ 15:01:38]# systemctl daemon-reload # 启用并启动服务 [root@centos7 ~ 15:02:00]# systemctl enable studyd.service --now Created symlink from /etc/systemd/system/multi-user.target.wants/studyd.service to /etc/systemd/system/studyd.service. # 查看服务状态 [root@centos7 ~ 15:02:23]# systemctl status studyd.service ● studyd.service - Study server daemon Loaded: loaded (/etc/systemd/system/studyd.service; enabled; vendor preset: disabled) Active: active (running) since 三 2026-07-22 15:02:23 CST; 22s ago Main PID: 3185 (study) Tasks: 2 CGroup: /system.slice/studyd.service ├─3185 /bin/bash /usr/local/bin/study └─3198 sleep 5 7月 22 15:02:23 centos7.cloud systemd[1]: Started Study server daemon.3.2.3 验证日志
[root@centos7 ~ 15:02:46]# tail -f /var/log/study.log 2026年 07月 22日 星期三 15:02:53 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:02:58 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:03:03 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:03:08 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:03:13 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:03:18 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:03:23 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:03:28 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:03:34 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:03:39 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:03:44 CST: I'M studying [ Linux ] 2026年 07月 22日 星期三 15:03:49 CST: I'M studying [ Linux ] ......3.2.4 修改配置(自我研究)
[root@centos7 ~ 15:13:44]# vim /usr/local/bin/study # 在配置文件中将Linux改为Linux&Pthon # 修改配置文件,需要重新开启服务 [root@centos7 ~ 15:17:42]# systemctl restart studyd.service # 如果修改了配置文件里面的文件地址,ExecStart=/usr/local/bin/study改为ExecStart=/usr/ # 需要重新启动daemon [root@centos7 ~ 15:17:42]# vim /etc/systemd/system/studyd.service [root@centos7 ~ 15:17:42]# systemctl daemon-reload # 再次查看日志文件内容 [root@centos7 ~ 15:02:46]# tail -f /var/log/study.log 2026年 07月 22日 星期三 15:35:56 CST: I'M studying [ Linux & Python ] 2026年 07月 22日 星期三 15:36:01 CST: I'M studying [ Linux & Python ] 2026年 07月 22日 星期三 15:36:06 CST: I'M studying [ Linux & Python ] 2026年 07月 22日 星期三 15:36:11 CST: I'M studying [ Linux & Python ] 2026年 07月 22日 星期三 15:36:16 CST: I'M studying [ Linux & Python ] 2026年 07月 22日 星期三 15:36:21 CST: I'M studying [ Linux & Python ] 2026年 07月 22日 星期三 15:36:26 CST: I'M studying [ Linux & Python ] 2026年 07月 22日 星期三 15:36:31 CST: I'M studying [ Linux & Python ] 2026年 07月 22日 星期三 15:36:36 CST: I'M studying [ Linux & Python ] ···········