從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
參考博客
https://blog.csdn.net/Mr_Yang__/article/details/84133783Centos7之Systemd(Service文件)詳解