# 用法: systemctl COMMAND name[.service]
# 启动服务: service name start ==> systemctl start name[.service]
[iyunv@zabbix system]# systemctl start httpd
# 停止服务: service name stop ==> systemctl stop name[.service]
[iyunv@zabbix system]# systemctl stop httpd
# 重启服务: service name restart ==> systemctl restart name[.service]
[iyunv@zabbix system]# systemctl restart httpd
# 状态: service name status ==> systemctl status name[.service]
[iyunv@zabbix system]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-02-26 10:30:40 CST; 23min ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 3870 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─3870 /usr/sbin/httpd -DFOREGROUND
├─3927 /usr/sbin/httpd -DFOREGROUND
├─3928 /usr/sbin/httpd -DFOREGROUND
├─3929 /usr/sbin/httpd -DFOREGROUND
├─3930 /usr/sbin/httpd -DFOREGROUND
└─3931 /usr/sbin/httpd -DFOREGROUND
Feb 26 10:30:40 zabbix.leistudy.com systemd[1]: Starting The Apache HTTP Server...
Feb 26 10:30:40 zabbix.leistudy.com systemd[1]: Started The Apache HTTP Server.
Feb 26 10:31:40 zabbix.leistudy.com systemd[1]: Reloaded The Apache HTTP Server.
Feb 26 10:32:20 zabbix.leistudy.com systemd[1]: Reloaded The Apache HTTP Server.
Feb 26 10:39:18 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
Feb 26 10:39:28 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
Feb 26 10:39:38 zabbix.leistudy.com systemd[1]: httpd.service: Got notification message from PID 3870, but reception is disabled.
# 条件式重启: service name condrestart ==> systemctl try-restart name[.service]
[iyunv@zabbix system]# systemctl try-restart httpd
# 重载或重启服务: systemctl reload-or-restart name[.service]
[iyunv@zabbix system]# systemctl reload-or-restart httpd
# 重载或条件式重启: systemctl reload-or-try-restart name[.service]
[iyunv@zabbix system]# systemctl reload-or-try-restart httpd
# 禁止设定为开机启动: systemctl mask name[.service]
[iyunv@zabbix system]# systemctl mask httpd
Created symlink from /etc/systemd/system/httpd.service to /dev/null.
# 取消禁止设定为开机自启: systemctl unmask name[.service]
[iyunv@zabbix system]# systemctl unmask httpd
Removed symlink /etc/systemd/system/httpd.service.
# 查看某服务当前激活状态: systemctl is-active name[.service]
[iyunv@zabbix system]# systemctl is-active httpd
active
# 查看所有已经激活的服务: systemctl list-units --type service
[iyunv@zabbix system]# systemctl list-units --type service
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
chronyd.service loaded active running NTP client/server
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
...
# 查看所有服务: systemctl list-unit --type service --all
[iyunv@zabbix system]# systemctl list-units --type service --all
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
brandbot.service loaded inactive dead Flexible Branding Service
chronyd.service loaded active running NTP client/server
cpupower.service loaded inactive dead Configure CPU power related settings
crond.service loaded active running Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
● display-manager.service not-found inactive dead display-manager.service
...
# 设定某服务开机自启动: chkconfig name on ==> systemctl enable name[.service]
[iyunv@zabbix system]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
# 设定某服务禁止开机自启动: chkconfig name off ==> systemctl disable name[.service]
[iyunv@zabbix system]# systemctl disable httpd
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
# 查看所有服务的开机自启状态: chkconfig --list ==> systemctl list-unit-files --type service
[iyunv@zabbix system]# systemctl list-unit-files --type service
UNIT FILE STATE
arp-ethers.service disabled
auditd.service enabled
autovt@.service enabled
blk-availability.service disabled
...
# 查看服务是否开机自启: systemctl is-enabled name[.service]
[iyunv@zabbix system]# systemctl is-enabled httpd
enabled
# 查看服务的依赖关系: systemctl list-dependencies name[.service]
[iyunv@zabbix system]# systemctl list-dependencies httpd
httpd.service
● ├─-.mount
● ├─system.slice
● └─basic.target
● ├─microcode.service
● ├─rhel-autorelabel-mark.service
● ├─rhel-autorelabel.service
● ├─rhel-configure.service
...
target unit
# 运行级别:
# level 0 ==> runlevel0.target, poweroff.target
# level 1 ==> runlevel1.target, rescue.target
# level 2 ==> runlevel2.target, multi-user.target
# level 3 ==> runlevel3.target, multi-user.target
# level 4 ==> runlevel4.target, multi-user.target
# level 5 ==> runlevel5.target, graphical.target
# level 6 ==> runlevel6.target, reboot.target
# 级别切换: init N ==> systemctl isolate name.service
[iyunv@zabbix ~]# systemctl isolate multi-user.target
# 查看级别: runlevel ==> systemctl list-units --type target
[iyunv@zabbix ~]# systemctl list-units --type target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
cryptsetup.target loaded active active Encrypted Volumes
getty.target loaded active active Login Prompts
local-fs-pre.target loaded active active Local File Systems (Pre)
local-fs.target loaded active active Local File Systems
multi-user.target loaded active active Multi-User System
network-online.target loaded active active Network is Online
network.target loaded active active Network
paths.target loaded active active Paths
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
timers.target loaded active active Timers
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.
15 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
# 获取默认运行级别: /etc/inittab ==> systemctl get-default
[iyunv@zabbix ~]# systemctl get-default
multi-user.target
# 修改默认级别: /etc/inittab ==> systemctl set-default name[.target]
[iyunv@zabbix ~]# systemctl set-default rescue.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/rescue.target.
# 切换至紧急救援模式: systemctl resuce
# 切换至emergency模式: systemctl emergency