顯示所有已啟用的Unit(list-units)
因為systemctl命令的默認選項是systemctl list-units,不添加任何選項的話,將顯示list-units的結果。
另外在systemctl命令中加上--no-pager選項后將不翻頁顯示所有單元的內容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$ systemctl list- units UNIT???????????????????????????????????????????? LOAD?? ACTIVE SUB?????? DESCRIPTION proc-sys-fs-binfmt_misc.automount??????????????? loaded active waiting?? Arbitrary Executable File Formats File System Automount Point sys-devices-platform-serial8250- tty -ttyS1.device loaded active plugged?? /sys/devices/platform/serial8250/tty/ttyS1 sys-devices-platform-serial8250- tty -ttyS2.device loaded active plugged?? /sys/devices/platform/serial8250/tty/ttyS2 sys-devices-platform-serial8250- tty -ttyS3.device loaded active plugged?? /sys/devices/platform/serial8250/tty/ttyS3 sys-devices-pnp0-00:09- tty -ttyS0.device????????? loaded active plugged?? /sys/devices/pnp0/00 :09 /tty/ttyS0 (--snip--) 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 . 96 loaded units listed. Pass --all to see loaded but inactive units , too. To show all installed unit files use 'systemctl list-unit-files' . |
統一顯示已安裝的Unit文件(list-unit-files)
1
2
3
4
5
6
7
8
9
10
11
12
|
$ systemctl list-unit-files UNIT FILE????????????????????????????? STATE proc-sys-fs-binfmt_misc.automount????? static dev-hugepages. mount ??????????????????? static sys-kernel-debug. mount ???????????????? static tmp. mount ????????????????????????????? masked var-lib-nfs-rpc_pipefs. mount ?????????? static brandbot.path????????????????????????? disabled arp-ethers.service???????????????????? disabled auditd.service???????????????????????? enabled (--snip--) 207 unit files listed. |
比如執行了yum install httpd命令安裝Apache后,會追加這樣一個Unit文件。
1
2
|
$ systemctl list-unit-files | grep httpd httpd.service????????????????????????? disabled |
啟用Unit(enable)
啟用Unit后,會如下所示系統在開機時就會啟動服務。systemctl enable httpd命令與之前CentOS6中chkonfig httpd on的作用是一樣的。
1
2
|
$ sudo systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
|
另外multi-user即相當于runlevel(init) 2 or 3 or 4。各運行狀態如下所示。
- runlevel0 -> poweroff 關機
- runlevel1 -> rescue 單用戶模式(救援模式)
- runlevel2 -> multi-user 多用戶模式(無網絡、字符界面)
- runlevel3 -> multi-user 多用戶模式(有網絡、字符界面)
- runlevel4 -> multi-user 備用模式(現已基本廢棄)
- runlevel5 -> graphical 圖形界面
- runlevel6 -> reboot 重啟系統
命令執行后,原本安裝后默認禁用(disable)的服務將被啟用。
1
2
|
$ systemctl list-unit-files --no-pager | grep httpd httpd.service????????????????????????? enabled |
禁用Unit(disable)
禁用后,該服務(啟動項)將被刪除。systemctl disable httpd與chkonfig httpd off作用等同。
1
2
|
$ sudo systemctl disable httpd Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
|
使用list-unit-files命令確認是否被禁用。
1
2
|
$ systemctl list-unit-files --no-pager | grep httpd httpd.service????????????????????????? disabled |
查看Unit是否啟用/禁用(is-enable)
啟用時顯示enabled,禁用時顯示disabled。
1
2
3
4
5
6
|
$ sudo systemctl is-enabled httpd disabled $ sudo systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
$ sudo systemctl is-enabled httpd enabled |
Unit的再啟用(reenable)
使用再啟動命令后,會先禁用Unit后再啟用。
1
2
3
|
$ sudo systemctl reenable httpd Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
|
啟動Unit(start)
service命令會顯示啟動過程,而systemctl則并不顯示啟動的詳細過程。
1
2
3
4
|
$ sudo systemctl start httpd $ ps aux | grep httpd root????? 7977? 0.3? 0.4 213704? 4880 ???????? Ss?? 01:02?? 0:00 /usr/sbin/httpd -DFOREGROUND apache??? 7978? 0.0? 0.2 213704? 2872 ???????? S??? 01:02?? 0:00 /usr/sbin/httpd -DFOREGROUND |
Unit的狀態確認(status)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$ sudo systemctl status httpd httpd.service - The Apache HTTP Server ??? Loaded: loaded ( /usr/lib/systemd/system/httpd .service; disabled) ??? Active: active (running) since 金 2014-10-10 01:06:07 UTC; 8s ago ? Main PID: 7998 (httpd) ??? Status: "Processing requests..." ??? CGroup: /system .slice /httpd .service ??????????? ├─7998 /usr/sbin/httpd -DFOREGROUND ??????????? ├─7999 /usr/sbin/httpd -DFOREGROUND ??????????? ├─8000 /usr/sbin/httpd -DFOREGROUND ??????????? ├─8001 /usr/sbin/httpd -DFOREGROUND ??????????? ├─8002 /usr/sbin/httpd -DFOREGROUND ??????????? └─8003 /usr/sbin/httpd -DFOREGROUND 10月 10 01:06:07 ip-172-31-7-131 systemd[1]: Starting The Apache HTTP Server... 10月 10 01:06:07 ip-172-31-7-131 systemd[1]: Started The Apache HTTP Server. |
顯示Unit的詳細信息(show)
show選項會顯示Unit的詳細信息。雖然status選項更加直觀,但在處理腳本的時候show選項使用起來更加得心應手。
1
2
3
4
5
6
7
8
9
10
11
12
|
$ sudo systemctl show httpd Id=httpd.service Names=httpd.service Requires=basic.target Wants=system.slice Conflicts= shutdown .target Before= shutdown .target After=network.target remote-fs.target nss-lookup.target systemd-journald.socket basic.target system.slice Description=The Apache HTTP Server LoadState=loaded ActiveState=active ... |
終止Unit(stop)
終止單元和其開啟過程一樣,并不顯示詳細信息。
1
|
$ sudo systemctl stop httpd |
重新讀取Unit的配置文件(reload)
是否可重新加載取決于Unit自身。
1
|
$ sudo systemctl reload httpd |
重啟Unit(restart)
和start、stop一樣,通常無消息提示。
1
|
$ sudo systemctl restart httpd |
嘗試重啟Unit(try-restart)
Unit處在啟動狀態時則重啟。Unit未運行時則不進行任何操作。
1
|
$ sudo systemctl try-restart httpd |
重新加載Unit設置或重啟(reload-or-restart)
若Unit能重新加載配置則執行reload操作,若無法重新加載配置則執行restart操作。另外Unit若處在停止狀態則啟動。
1
|
$ sudo systemctl reload-or-restart httpd |
重新載入Unit的配置或嘗試重啟(reload-or-try-restart)
雖然與reload-or-restart同樣,但若Unit處在停止狀態時則不啟動。
1
2
3
4
5
|
$ sudo systemctl reload-or-try-restart httpd $ sudo systemctl stop httpd $ sudo systemctl reload-or-try-restart httpd Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details. |
強行終止Unit(kill)
1
|
$ sudo systemctl kill httpd |
Unit的鎖定(遮掩)(mask/unmask)
雖然禁用(disable)Unit后也能啟動它,但使用mask命令后該服務則完全無法啟動。
1
2
3
4
|
$ sudo systemctl mask httpd ln -s '/dev/null' '/etc/systemd/system/httpd.service' $ sudo systemctl start httpd Failed to issue method call: Unit httpd.service is masked. |
Unit被masked后,is-enabled會提示被鎖定。
1
2
|
$ sudo systemctl is-enabled httpd masked |
解除mask需要使用unmask選項。
1
2
|
$ sudo systemctl unmask httpd rm '/etc/systemd/system/httpd.service' |
確認Unit的運行狀態(is-active)
使用is-active選項,若Unit處在運行狀態則反饋active。
1
2
3
|
$ sudo systemctl start httpd $ sudo systemctl is-active httpd active |
若未處在運行狀態則反饋unknown。
1
2
3
|
$ sudo systemctl stop httpd $ sudo systemctl is-active httpd unknown |
查看Unit的異常狀態(is-failed)
使用選項is-failed,若Unit正常運行則反饋active。
1
2
|
$ sudo systemctl is-failed httpd active |
若該Unit異?;蛭磫?,則反饋failed。
1
2
3
4
5
|
$ sudo mv /etc/httpd/conf/httpd .conf /etc/httpd/conf/httpd .conf.org $ sudo systemctl restart httpd Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details. $ sudo systemctl is-failed httpd failed |
重置Unit的異常狀態(reset-failed)
systemd將重置處在異常狀態中的Unit。
1
2
3
4
5
|
$ sudo systemctl is-failed httpd failed $ sudo systemctl reset-failed httpd $ sudo systemctl is-failed httpd unknown |
查看Unit的依賴關系(list-dependencies)
將顯示該Unit所依賴的所有Unit。
1
2
3
4
5
6
7
8
9
10
11
12
|
$ sudo systemctl list-dependencies httpd httpd.service ├─system.slice └─basic.target ?? ├─microcode.service ?? ├─rhel-autorelabel-mark.service ?? ├─rhel-autorelabel.service ?? ├─rhel-configure.service ?? ├─rhel-dmesg.service ?? ├─rhel-loadmodules.service ?? ├─paths.target ?? ├─slices.target |
寫在最后
從CentOS 6.x過來的用戶可能完全習慣了init的管理方式,對于新systemd的管理方式可能難以適應或心生厭惡。但由于systemd的強大和不斷普及,今后將很有可能成為發展主流,所以有必要深入學習并習慣這種新管理方式。
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/100908