从service文件说起
之前的linux系统,从开机到启动初始化进程,要经过:
BIOS -> Boot Loader -> 加载系统内核 -> 对内核初始化 -> 启动初始化进程,提供工作环境
现有的systemd采用了并发启动机制,提升了开机速度。它将原来的初始化进程改为了systemd管理下的目标.target,并用systemctl来管理服务。
service服务启动关闭过程
1.设置httpd服务开机启动
systemctl enable httpd
返回信息
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
可以看到这里是在/etc/下那个目录创建了一个软链接(symbolic link)从/usr/lib/...里,没说反,下面有验证
2.查看服务状态
systemctl status httpd
返回信息
Active:inactive(dead)
3.启动服务
systemctl start httpd
状态更新为
active (running)
相比之前还多了
Main PID:1079(httpd)
status:"Processing requests..."
Tasks:6
Memory:2.9M
CGroup:...(每个httpd化进程占用的端口号和状态)
4.停止httpd服务
systemctl stop httpd.service
再次查看status变回inactive了
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Sat 2020-07-25 09:23:48 CST; 2s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 3578 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 1079 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 1079 (code=exited, status=0/SUCCESS)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
如果停不下来了,用kill杀掉
systemctl kill httpd.service
5.前面设置了开机启动,现在把它关掉
systemctl disable httpd
返回结果
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
就是把创建的软链接删除了
6.[.server]文件里面是什么
cat httpd.server
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target 启动顺序
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd 读取自己的环境参数
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND 启动进程时执行的命令
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful $OPTIONS来自上面环境参数文件
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target 表示httpd服务所在的Target(服务组)
它分为3个模块:
[Unit]是启动顺序和依赖关系,如果有Wants或Requires字段代表依赖关系
[Service]是启动的行为
[install]是如何安装这个配置文件
.server文件还有很多东西,这里就针对httpd说一下,剩下的遇到了再研究
验证:查看下步骤1.enable里的目录
插一句,简单说下软链接的创建
ln -s file file_ln
-s 创建软链接 没有这个参数就是创建硬链接
接下来先去看下它操作的.service文件
cd /etc/systemd/system/multi-user.target.wants
ll
显示为lrwxrwxrwx的链接文件,原文件在/usr/lib/systemd/system/里
再到/usr/lib/systemd/system/里,找到了httpd.service
-rw-r--r-- 1 root root 752 Nov 27 2019 httpd.service