service與systemctl命令比較


本文將比較 linux 的 service 和 systemctl 命令,先分別簡單介紹這兩個命令的基礎用法,然后進行比較。

從 CentOS 7.x 開始,CentOS 開始使用 systemd 服務來代替 daemon,原來管理系統啟動和管理系統服務的相關命令全部由 systemctl命 令來代替。

 

service 命令

service命令是Redhat Linux兼容的發行版中用來控制系統服務的實用工具,它以啟動、停止、重新啟動和關閉系統服務,還可以顯示所有系統服務的當前狀態。

語法: service < option > | --status-all | [ service_name [ command | --full-restart ] ]

option 的值

  • -h:顯示 service 的幫助信息

  • -status:顯示所服務的狀態

  • --status-all:查看所有服務的狀態

  • service_name:服務名,即 /etc/init.d 目錄下的腳本文件名

  • command:系統服務腳本支持的控制命令,如:start、stop 和 restart

  • --full-restart:重啟所有服務

 

實例:查看 service 的幫助信息

1
2
3
[root@localhost ~] # service -h
Usage: service < option > | --status-all | [ service_name [  command  | --full-restart ] ]
[root@localhost ~] #

 

實例2:查看所有的服務狀態

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@centos-x64 ~] # service --status-all
auditd (pid  1299) is running...
Stopped
cgred is stopped
crond (pid  1481) is running...
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt  source                destination
1    ACCEPT     all      :: /0                  :: /0                 state RELATED,ESTABLISHED
2    ACCEPT     icmpv6    :: /0                  :: /0
3    ACCEPT     all      :: /0                  :: /0
4    ACCEPT     tcp      :: /0                  :: /0                 state NEW tcp dpt:22
5    ACCEPT     tcp      :: /0                  :: /0                 state NEW tcp dpt:80
6    REJECT     all      :: /0                  :: /0                 reject-with icmp6-adm-prohibited
Chain FORWARD (policy ACCEPT)
num  target     prot opt  source                destination
1    REJECT     all      :: /0                  :: /0                 reject-with icmp6-adm-prohibited

 

實例3: 使用 service 啟動/重啟/停止網絡服務

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@centos-x64 ~] # service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
 
[root@centos-x64 ~] # service network start
Bringing up loopback interface:               [ OK ]
Bringing up interface eth0:                   [ OK ]
 
[root@centos-x64 ~] # service network stop
Bringing dwon interface eth0:                   [ OK ]
Bringing down loopback interface:               [ OK ]
 
[root@centos-x64 ~] # service network status
Configured devices:
lo eth0
Currently active devices:
lo eth0

 

 

systemctl 命令

歷史上,Linux 的啟動一直采用init進程。下面的命令用來啟動服務。

1
2
3
sudo  /etc/init .d /apache2  start
# 或者
$ service apache2 start

這種方法有兩個缺點:

  • 一是啟動時間長。init 進程是串行啟動,只有前一個進程啟動完,才會啟動下一個進程。

  • 二是啟動腳本復雜。init 進程只是執行啟動腳本,不管其他事情。腳本需要自己處理各種情況,這往往使得腳本變得很長

 

Systemd 就是為了解決上面問題而誕生的。它的設計目標是,為系統的啟動和管理提供一套完整的解決方案。根據 Linux 慣例,字母 d 是守護進程(daemon)的縮寫。 Systemd 這個名字的含義,就是它要守護整個系統。使用了 Systemd,就不需要再用 init 了。Systemd 取代了 initd,成為系統的第一個進程(PID 等於 1),其他進程都是它的子進程。

1
2
3
4
[root@localhost ~] # systemctl --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
[root@localhost ~] #

 

Systemd 的優點是功能強大,使用方便,缺點是體系龐大,非常復雜。事實上,現在還有很多人反對使用 Systemd,理由就是它過於復雜,與操作系統的其他部分強耦合,違反 “keep simple, keep stupid” 的Unix 哲學。

 

實例1:systemctl常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 重啟系統
sudo  systemctl reboot
# 關閉系統,切斷電源
sudo  systemctl poweroff
# CPU停止工作
sudo  systemctl halt
# 暫停系統
sudo  systemctl suspend
# 讓系統進入冬眠狀態
sudo  systemctl hibernate
# 讓系統進入交互式休眠狀態
sudo  systemctl hybrid- sleep
# 啟動進入救援狀態(單用戶狀態)
sudo  systemctl rescue

 

實例2:檢查systemd和systemctl的二進制文件和庫的安裝位置。

1
2
3
4
# whereis systemd 
systemd:  /usr/lib/systemd  /etc/systemd  /usr/share/systemd  /usr/share/man/man1/systemd .1.gz
# whereis systemctl
systemctl:  /usr/bin/systemctl  /usr/share/man/man1/systemctl .1.gz

 

實例3:檢查systemd是否正在運行

1
2
3
4
5
6
# ps -eaf | grep [s]ystemd
root         1     0  0 16:27 ?        00:00:00  /usr/lib/systemd/systemd  --switched-root --system --deserialize 23
root       444     1  0 16:27 ?        00:00:00  /usr/lib/systemd/systemd-journald
root       469     1  0 16:27 ?        00:00:00  /usr/lib/systemd/systemd-udevd
root       555     1  0 16:27 ?        00:00:00  /usr/lib/systemd/systemd-logind
dbus       556     1  0 16:27 ?        00:00:00  /bin/dbus-daemon  --system --address=systemd: --nofork --nopidfile --systemd-activation

 

實例4:列出所有服務(包括啟用和禁用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# systemctl list-unit-files --type=service
UNIT FILE                                   STATE
arp-ethers.service                          disabled
auditd.service                              enabled
autovt@.service                             disabled
blk-availability.service                    disabled
brandbot.service                            static
collectd.service                            disabled
console-getty.service                       disabled
console-shell.service                       disabled
cpupower.service                            disabled
crond.service                               enabled
dbus-org.fedoraproject.FirewallD1.service   enabled
....

 

service 與 systemctl 命令對比

daemon命令 systemctl命令 說明
service [服務] start systemctl start [unit type] 啟動服務
service [服務] stop systemctl stop [unit type] 停止服務
service [服務] restart systemctl restart [unit type] 重啟服務


免責聲明!

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



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