Linux 服务管理windows 开机的时候有些程序自动启动了。命令行运行services.mscsystemd 介绍基本概念CentOS 7 使用 Systemd 引导系统启动速度最快所有进程无论有无依赖关系则都是并行启动很多时候进程没有真正启动而是只有一个信号或者说是标记而已在真正利用的时候才会真正启动。Systemd为了解决上文的问题而诞生。它的目标是为系统的启动和管理提供一套完整的解决方案。系统引导程序用户空间systemdpid为1内核空间kthreaddpid为2服务从业务角度来称呼例如 web 服务数据库服务。守护进程daemonweb 服务器对外提供 web 服务由 web 相关的进程提供支持。以web服务为例服务web服务守护进程httpd万和提供培训服务服务万培训云计算守护进程课程顾问、代课老师、就业老师等。例如# 安装软件包 [rootcentos7 ~ 19:04:32]# yum install -y httpd # 启动服务 [rootcentos7 ~ 19:05:18]# systemctl start httpd # 查看进程 [rootcentos7 ~ 19:05:47]# ps -C httpd f PID TTY STAT TIME COMMAND 5271 ? Ss 0:00 /usr/sbin/httpd -DFOREGROUND 5288 ? S 0:00 \_ /usr/sbin/httpd -DFOREGROUND 5289 ? S 0:00 \_ /usr/sbin/httpd -DFOREGROUND 5290 ? S 0:00 \_ /usr/sbin/httpd -DFOREGROUND 5291 ? S 0:00 \_ /usr/sbin/httpd -DFOREGROUND 5292 ? S 0:00 \_ /usr/sbin/httpd -DFOREGROUNDhttpd 服务对应的守护进程是5271、5288…systemd 架构守护进程systemd由守护进程干活。工具systemctl提供给用户要、用来根机器交互的工具。unit 类型systemctl 命令用于管理不同类型的系统对象这些对象称之为units。Service unit用于定义系统服务文件扩展名为**.service** 例如httpd.serviceSocket unit用于标识进程间通信用的 socket文件文件扩展名为.socketTarget unit用于模拟实现“运行级别”文件扩展名为.targetTimer unit用于管理计划任务文件扩展名为.timerDevice unit用于定义内核识别的设备文件扩展名为.deviceMount unit用于定义文件系统挂载点文件扩展名为.mountSnapshot unit管理系统快照文件扩展名为.snapshotSwap unit用于标识swap设备文件扩展名为.swapAutomount unit文件系统的自动挂载点文件扩展名为.automountPath unit用于根据文件系统上特定对象的变化来启动其他服务文件扩展名为.pathSlice unit用于资源管理文件扩展名为.slice查看 unit 列表信息# 列出状态为loaded units [rootcentos7 ~ 19:06:23]# systemctl list-unitssystemctl list-units命令输出说明UNIT服务单元名称。LOADsystemd是否正确解析了单元的配置并将该单元加载到内存中。ACTIVE单元的高级别激活状态。此信息表明单元是否已成功启动。SUB单元的低级别激活状态。此信息指示有关该单元的更多详细信息。信息视单元类型、 状态以及单元的执行方式而异。DESCRIPTION单元的简短描述。# -t选项查看特定类型unit 清单 [dengcentos7 ~ 19:07:55]$ systemctl list-units -t timer UNIT LOAD ACTIVE SUB DESCRIPTION systemd-tmpfiles-clean.timer loaded active waiting Daily Cleanup of Temporary Directories unbound-anchor.timer loaded active waiting daily update of the root trust anchor for DNSSEC 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 [rootcentos7 ~ 19:09:12]# systemctl list-units --type service --all # 列出系统中所有unit包括未loaded的unit [rootcentos7 ~ 19:10:38]# systemctl list-unit-files # 查看失败的服务 [rootcentos7 ~ 19:11:05]# systemctl --failed --type service查看单个 unit 信息[dengcentos7 ~ 19:12:44]$ 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 17:37:45 CST; 5h 50min ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 1167 (sshd) Tasks: 1 CGroup: /system.slice/sshd.service └─1167 /usr/sbin/sshd -D ......关键字概述loaded单元配置文件已处理active(running)正在运行active(exited)已成功完成一次性配置active(waiting)运行中正在等待事件inactive不再运行enabled系统引导时启动disabled系统引导时不启动static无法启动依赖其他单元启动控制系统服务命令任务systemctl statusUNIT查看单元状态的详细信息。systemctl stopUNIT在运行中的系统上停止一项服务。systemctl startUNIT在运行中的系统上启动一项服务。systemctl restartUNIT在运行中的系统上重新启动一项服务。systemctl reloadUNIT重新加载运行中服务的配置文件。systemctl maskUNIT禁用服务使其无法手动启动或在系统引导时启动。systemctl unmaskUNIT使屏蔽的服务变为可用。systemctl enableUNIT将服务配置为在系统引导时启动。使用--now选项也会启动该服务。systemctl disableUNIT禁止服务在系统引导时启动。使用--now选项也会停止该服务。# 停止服务 [rootcentos7 ~ 19:14:21]# systemctl stop sshd.service # 客户端连接测试 [rootcentos7 ~ 19:14:56]# ssh dengcentos7 hostname # 启动服务 [rootcentos7 ~ 19:15:22]# systemctl start sshd.service # 客户端连接测试 [rootcentos7 ~ 19:15:48]# ssh dengcentos7 hostname # 重启服务相当于stop再start [rootcentos7 ~ 19:16:15]# systemctl restart sshd.service # 一般用于配置文件变动后重新加载 [rootcentos7 ~ 19:16:43]# systemctl reload sshd.service # 重新加载服务服务对应的主进程不会重启只会重新加载一次配置文件。 # 禁止服务开机自启 [rootcentos7 ~ 19:17:09]# systemctl disable httpd.service # 允许服务开机自启 [rootcentos7 ~ 19:17:35]# systemctl enable httpd.service # 查看服务是否开机自启 [rootcentos7 ~ 19:18:02]# systemctl is-enabled httpd.service enabled # 查看服务是否正在运行 [rootcentos7 ~ 19:18:28]# systemctl is-active httpd.service active # 屏蔽服务禁止启动 [rootcentos7 ~ 19:18:54]# systemctl mask httpd.service # 取消屏蔽 [rootcentos7 ~ 19:19:21]# systemctl unmask httpd.serviceunit 配置文件配置文件说明systemd 单元配置文件通常位于以下目录/usr/lib/systemd/system/软件包安装的单元文件不要修改/etc/systemd/system/管理员创建或修改的单元文件优先使用/run/systemd/system/运行时生成的临时单元文件配置文件结构[Unit] Description描述信息 Afternetwork.target Wantsnetwork.target [Service] Typesimple ExecStart/path/to/executable Restartalways Userdeng Groupdeng [Install] WantedBymulti-user.target段说明[Unit]定义单元的元数据和依赖关系[Service]服务单元的配置仅适用于.service文件[Install]定义单元的安装信息开发一个 study 服务创建一个自定义服务实现每隔1秒向日志文件写入一条消息。# 创建脚本 [dengcentos7 ~ 19:20:47]$ cat /usr/local/bin/study.sh EOF #!/bin/bash while true do echo $(date): Im studying Linux service management /var/log/study.log sleep 1 done EOF [dengcentos7 ~ 19:21:14]$ chmod x /usr/local/bin/study.sh # 创建服务配置文件 [rootcentos7 ~ 19:21:40]# cat /etc/systemd/system/study.service EOF [Unit] DescriptionStudy Service Afternetwork.target [Service] Typesimple ExecStart/usr/local/bin/study.sh Restartalways Userdeng Groupdeng [Install] WantedBymulti-user.target EOF # 重新加载systemd配置 [rootcentos7 ~ 19:22:06]# systemctl daemon-reload # 启动服务 [rootcentos7 ~ 19:22:33]# systemctl start study.service # 查看服务状态 [rootcentos7 ~ 19:22:59]# systemctl status study.service ● study.service - Study Service Loaded: loaded (/etc/systemd/system/study.service; disabled; vendor preset: enabled) Active: active (running) since 三 2026-07-22 19:22:33 CST; 26s ago Main PID: 12345 (study.sh) Tasks: 2 CGroup: /system.slice/study.service ├─12345 /bin/bash /usr/local/bin/study.sh └─12367 sleep 1 # 查看日志 [dengcentos7 ~ 19:23:25]$ tail -f /var/log/study.log 2026年 07月 22日 星期三 19:22:33 CST: Im studying Linux service management 2026年 07月 22日 星期三 19:22:34 CST: Im studying Linux service management 2026年 07月 22日 星期三 19:22:35 CST: Im studying Linux service management ...... # 设置开机自启 [rootcentos7 ~ 19:24:01]# systemctl enable study.service # 停止服务 [rootcentos7 ~ 19:24:28]# systemctl stop study.service验证日志[dengcentos7 ~ 19:25:04]$ cat /var/log/study.log 2026年 07月 22日 星期三 19:22:33 CST: Im studying Linux service management 2026年 07月 22日 星期三 19:22:34 CST: Im studying Linux service management 2026年 07月 22日 星期三 19:22:35 CST: Im studying Linux service management 2026年 07月 22日 星期三 19:22:36 CST: Im studying Linux service management 2026年 07月 22日 星期三 19:22:37 CST: Im studying Linux service management ...... studying Linux service management 2026年 07月 22日 星期三 19:22:34 CST: Im studying Linux service management 2026年 07月 22日 星期三 19:22:35 CST: Im studying Linux service management 2026年 07月 22日 星期三 19:22:36 CST: Im studying Linux service management 2026年 07月 22日 星期三 19:22:37 CST: Im studying Linux service management ......