systemd進程管理
systemd管理的優勢
1.最新系統都采用systemd管理(RedHat7,CentOS7,Ubuntu15...)
2.CentOS7 支持開機並行啟動服務,顯著提高開機啟動效率
3.CentOS7關機只關閉正在運行的服務,而CentOS6,全部都關閉一次。
4.CentOS7服務的啟動與停止不再使用腳本進行管理,也就是/etc/init.d下不在有腳本。
5.CentOS7使用systemd解決原有模式缺陷,比如原有service不會關閉程序產生的子進程。
systemd相關文件
systemd控制的相關文件 | CentOS6 | CentOS7 |
---|---|---|
服務啟動的腳本啟動路徑 | /etc/init.d | /usr/lib/systemd/system |
開機自啟服務存放路徑 | /etc/rcN.d | /etc/systemd/system/multi-user.target.wants/ |
默認運行級別配置文件 | /etc/inittab | /etc/systemd/system/default.target |
CentOS7安裝yum安裝nginx的默認腳本啟動路徑
[root@gong ~]# ll /usr/lib/systemd/system/nginx.service
-rw-r--r-- 1 root root 618 Oct 3 2019 /usr/lib/systemd/system/nginx.service
CentOS7開機自啟動所在的目錄,該目錄包含在該級別啟動時自啟動的服務
[root@gong ~]# ll /etc/systemd/system/multi-user.target.wants/
CentOS7默認運行級別的控制,把它鏈接到/usr/lib/systemd/system/下面的不同級別,可以實現對不同默認級別的更改
[root@gong ~]# ll /etc/systemd/system/default.target
lrwxrwxrwx 1 root root 41 Apr 23 15:56 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target
systemd相關命令
CentOS6 | systemd CentOS7 | 作用 |
---|---|---|
/etc/init.d/nginx start | systemctl start nginx | 啟動nginx服務 |
/etc/init.d/nginx stop | systemctl stop nginx | 停止nginx服務 |
/etc/init.d/nginx status | systemctl status nginx | 查看nginx的狀態 |
/etc/init.d/nginx restart | systemctl restart nginx | 重啟nginx服務 |
/etc/init.d/nginx reload | systemctl reload nginx | 不停止nginx服務,重新加載配置文件 |
systemctl is-active nginx | 判斷nginx服務是否存活 | |
systemctl mask nginx | 禁止運行服務 | |
systemctl unmask nginx | 取消禁止運行服務 |
CentOS7啟動服務
[root@gong ~]# systemctl start nginx
CentOS7關閉服務
[root@gong ~]# systemctl stop nginx
CentOS7查看服務狀態
[root@gong ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Sat 2020-04-25 08:55:55 CST; 12s ago
Process: 7063 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 7060 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 7056 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 7064 (code=exited, status=0/SUCCESS)
Apr 25 08:55:44 gong systemd[1]: Starting The nginx HTTP and reverse proxy server...
Apr 25 08:55:45 gong nginx[7060]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr 25 08:55:45 gong nginx[7060]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 25 08:55:45 gong systemd[1]: Started The nginx HTTP and reverse proxy server.
Apr 25 08:55:55 gong systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Apr 25 08:55:55 gong systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Centos7重啟服務
[root@gong ~]# systemctl restart nginx
CentOS7不重啟服務重新加載配置文件
[root@gong ~]# systemctl reload nginx
CentOS7檢查服務是否啟動
[root@gong ~]# systemctl is-active nginx
active
禁止取消服務運行
[root@gong ~]# systemctl mask nginx
Created symlink from /etc/systemd/system/nginx.service to /dev/null.
[root@gong ~]# systemctl unmask nginx
Removed symlink /etc/systemd/system/nginx.service.
systemd開機自啟動相關命令
CentOS6 | CentOS7 | 作用 |
---|---|---|
chkconfig --list | systemctl list-unit-files | 查看開機自啟的服務 |
chkconfig nginx on | systemctl enable nginx | 開機自啟 |
chkconfig nginx off | systemctl disable nginx | 關閉開機自啟 |
chkconfig --list nginx | systemctl is-enabled nginx | 查看指定的服務是否被開啟 |
chkconfig --add nginx | systemctl daemon-reload | 當手寫腳本的時候讓系統認識 |
CentOS7查看開機自啟動
[root@gong ~]# systemctl list-unit-files
CentOS7設置開機自啟,實質上實在創建軟鏈接
[root@gong ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
CentOS7關閉開機自啟
[root@gong ~]# systemctl disable nginx
Removed symlink /etc/systemd/system/multi-user.target.wants/nginx.service.
CentOS7查看指定的服務是否被開啟
[root@gong ~]# systemctl is-enabled nginx
disabled
systemd服務狀態
服務狀態 | 狀態說明 |
---|---|
loaded | 服務單元的配置文件已經被處理 |
active(running) | 服務的一個或多個進程在運行中 |
active(exited) | 一次性運行的服務成功被執行並退出(服務運行后完成任務,相關進程會自動退出) |
active(waiting) | 服務已經運行但在等待某個事件 |
inactive | 服務沒有在運行 |
enable | 服務設定為開機運行 |
disabled | 服務設定為開機不運行 |
static | 服務不能被設定開機啟動,但可以由其他服務啟動該服務 |