Linux 服務管理兩種方式service和systemctl


轉載

參考地址:https://www.cnblogs.com/shijingjing07/p/9301590.html

https://my.oschina.net/ssdlinux/blog/3086218

https://blog.csdn.net/qq_38265137/article/details/83081881

 

Linux 服務管理兩種方式service和systemctl

1.service命令
service命令其實是去/etc/init.d目錄下,去執行相關程序

# service命令啟動redis腳本 service redis start # 直接啟動redis腳本 /etc/init.d/redis start # 開機自啟動 update-rc.d redis defaults

其中腳本需要我們自己編寫

2.systemctl命令
systemd是Linux系統最新的初始化系統(init),作用是提高系統的啟動速度,盡可能啟動較少的進程,盡可能更多進程並發啟動。
systemd對應的進程管理命令是systemctl

1)systemctl命令兼容了service
即systemctl也會去/etc/init.d目錄下,查看,執行相關程序

systemctl redis start systemctl redis stop # 開機自啟動 systemctl enable redis

systemctl 管理服務的啟動、重啟、停止、重載、查看狀態等常用命令區分

System V init 命令(RHEL 6)	systemctl 命令(RHEL 7)	作用
service foo start	systemctl start foo.service	啟動服務
service foo restart	systemctl restart foo.service	重啟服務
service foo stop	systemctl stop foo.service	停止服務
service foo reload	systemctl reload foo.service	重新加載配置文件(不終止服務)
service foo status	systemctl status foo.service	查看服務狀態

systemctl 設置服務開機啟動、不啟動、查看各級別下服務啟動狀態等常用命令

System V init 命令(RHEL 6)	systemctl 命令(RHEL 7)	作用
chkconfig foo on	systemctl enable foo.service	開機自動啟動
chkconfig foo off	systemctl disable foo.service	開機不自動啟動
chkconfig foo	systemctl is-enabled foo.service	查看特定服務是否為開機自動啟動
chkconfig --list	systemctl list-unit-files --type=service	查看各個級別下服務的啟動與禁用情況


Systemctl和service、chkconfig命令的關系

systemctl命令:是一個systemd工具,主要負責控制systemd系統和服務管理器。
service命令:可以啟動、停止、重新啟動和關閉系統服務,還可以顯示所有系統服務的當前狀態。
chkconfig命令:是管理系統服務(service)的命令行工具。所謂系統服務(service),就是隨系統啟動而啟動,隨系統關閉而關閉的程序。
systemctl命令是系統服務管理器指令,它實際上將 service 和 chkconfig 這兩個命令組合到一起。

systemctl是RHEL 7 的服務管理工具中主要的工具,它融合之前service和chkconfig的功能於一體。可以使用它永久性或只在當前會話中啟用/禁用服務。

所以systemctl命令是service命令和chkconfig命令的集合和代替。

例如:使用service啟動服務實際上也是調用systemctl命令。

[root@localhost ~]# service httpd start
Redirecting to /bin/systemctl start httpd.service

systemctl命令的用法
Systemctl命令簡介:
Systemctl是一個systemd工具,主要負責控制systemd系統和服務管理器。

Systemd是一個系統管理守護進程、工具和庫的集合,用於取代System V初始進程。Systemd的功能是用於集中管理和配置類UNIX系統。

systemd即為system daemon,是linux下的一種init軟件。

Systemctl命令常見用法:
(1)列出所有可用單元:

[root@localhost ~]# 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 
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 

(2)列出所有可用單元:

[root@localhost ~]# systemctl list-units
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary 
sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda
sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda
sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:0-2:0:0:0-block-sda
sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb
sys-devices-pci0000:00-0000:00:10.0-host2-target2:0:1-2:0:1:0-block-sdb
sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loade
sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device lo
..............

(3)列出所有失敗單元:

[root@localhost ~]# systemctl --failed 
UNIT LOAD ACTIVE SUB DESCRIPTION
● network.service loaded failed failed LSB: Bring up/down networking
● teamd@team0.service loaded failed failed Team Daemon for device team0

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'.

(4)檢查某個單元是否啟動:

[root@localhost ~]# systemctl is-enabled httpd.service 
enabled

(5)檢查某個服務的運行狀態:

[root@localhost ~]# systemctl status httpd.service 
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 日 2018-10-14 18:21:46 CST; 1 day 2h ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 19020 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─19020 /usr/sbin/httpd -DFOREGROUND
├─27310 /usr/sbin/httpd -DFOREGROUND
├─27311 /usr/sbin/httpd -DFOREGROUND
├─27312 /usr/sbin/httpd -DFOREGROUND
├─27313 /usr/sbin/httpd -DFOREGROUND
└─27314 /usr/sbin/httpd -DFOREGROUND

10月 14 18:21:46 localhost systemd[1]: Starting The Apache HTTP Serv....
10月 14 18:21:46 localhost httpd[19020]: AH00558: httpd: Could not r...e

(6)列出所有服務:

[root@localhost ~]# systemctl list-unit-files --type=service
UNIT FILE STATE 
abrt-ccpp.service enabled 
abrt-oops.service enabled 
abrt-pstoreoops.service disabled
abrt-xorg.service enabled 
abrtd.service enabled 
accounts-daemon.service enabled 
alsa-restore.service static 
alsa-state.service static 
alsa-store.service static 
arp-ethers.service disabled
atd.service disabled
auditd.service enabled 
auth-rpcgss-module.service static 

(7)啟動,停止,重啟服務等:

[root@localhost ~]# systemctl restart httpd.service
# systemctl restart httpd.service
# systemctl stop httpd.service
# systemctl reload httpd.service
# systemctl status httpd.service

(8)查詢服務是否激活,和配置是否開機啟動:

[root@localhost ~]# systemctl is-active httpd
active
[root@localhost ~]# systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
[root@localhost ~]# systemctl enable httpd 
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

(9)使用systemctl命令殺死服務:

[root@localhost ~]# systemctl kill httpd

(10)列出系統的各項服務,掛載,設備等:

[root@localhost ~]# systemctl list-unit-files --type 
automount device path snapshot swap timer
busname mount service socket target 

(11)獲得系統默認啟動級別和設置默認啟動級別:

[root@localhost ~]# systemctl get-default 
graphical.target
[root@localhost ~]# systemctl set-default multi-user.target 

(12)啟動運行等級:

systemctl isolate multiuser.target
1
(13)重啟、停止,掛起、休眠系統等:

# systemctl reboot
# systemctl halt
# systemctl suspend
# systemctl hibernate
# systemctl hybrid-sleep

Service命令用法
service命令可以啟動、停止、重新啟動和關閉系統服務,還可以顯示所有系統服務的當前狀態。

service命令的作用是去/etc/init.d目錄下尋找相應的服務,進行開啟和關閉等操作。

使用示例:

開啟關閉一個服務:service httpd start/stop

[root@localhost ~]# service httpd start
Redirecting to /bin/systemctl start httpd.service

查看系統服務的狀態:service --status-all

[root@localhost ~]# service --status-all
未加載 netconsole 模塊
已配置設備:
lo ens33 ens33.old team0 team0-port1 team0-port1.old team0-port2 team0-port2.old team0.old
當前活躍設備:
lo ens33 virbr0 ens38 ens39 team0
● rhnsd.service - LSB: Starts the Spacewalk Daemon
Loaded: loaded (/etc/rc.d/init.d/rhnsd; bad; vendor preset: disabled)
Active: active (running) since 五 2018-10-12 14:53:19 CST; 3 days ago
Docs: man:systemd-sysv-generator(8)
Main PID: 1380 (rhnsd)
CGroup: /system.slice/rhnsd.service
└─1380 rhnsd

chkconfig命令用法
chkconfig是管理系統服務(service)的命令行工具。所謂系統服務(service),就是隨系統啟動而啟動,隨系統關閉而關閉的程序。

chkconfig可以更新(啟動或停止)和查詢系統服務(service)運行級信息。更簡單一點,chkconfig是一個用於維護/etc/rc[0-6].d目錄的命令行工具。

chkconfig常見用法:

[root@localhost ~]# chkconfig --help
chkconfig 版本 1.7.2 - 版權 (C) 1997-2000 紅帽公司
在 GNU 公共許可條款下,本軟件可以自由重發行。

用法: chkconfig [--list] [--type <類型>] [名稱]
chkconfig --add <名稱>
chkconfig --del <名稱>
chkconfig --override <名稱>
chkconfig [--level <級別>] [--type <類型>] <名稱> <on|off|reset|resetpriorities>

(一)設置service開機是否啟動:

chkconfig name on/off/reset

on、off、reset用於改變service的啟動信息。
on表示開啟,off表示關閉,reset表示重置。
默認情況下,on和off開關只對運行級2,3,4,5有效,reset可以對所有運行級有效。

[root@localhost ~]# chkconfig httpd on
注意:正在將請求轉發到“systemctl enable httpd.service”

在Redhat7上,運行chkconfig命令,都會被轉到systemcle命令上。

(2)設置service運行級別:

chkconfig --level levels

該命令可以用來指定服務的運行級別,即指定運行級別2,3,4,5等。

等級0表示:表示關機
等級1表示:單用戶模式
等級2表示:無網絡連接的多用戶命令行模式
等級3表示:有網絡連接的多用戶命令行模式
等級4表示:不可用
等級5表示:帶圖形界面的多用戶模式
等級6表示:重新啟動
例如:

[root@localhost ~]# chkconfig --level 5 httpd on
注意:正在將請求轉發到“systemctl enable httpd.service”

(三)列出service啟動信息:

# chkconfig --list [name]

如果不指定name,會列出所有services的信息。

每個service每個運行級別都會有一個啟動和停止腳本;當切換運行級別時,init不會重啟已經啟動的service,也不會重新停止已經停止的service。

例如:

[root@localhost ~]# chkconfig --list

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
如果您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
欲查看對特定 target 啟用的服務請執行
'systemctl list-dependencies [target]'。

netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
rhnsd 0:關 1:關 2:開 3:開 4:開 5:開 6:關
**總結:**service命令的功能基本都被systemct取代。直接使用systemctl命令即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM