计算机网络/计算机科学与应用/系统/运维/开发

Systemctl 命令管理服务

在CentOS 5和CentOS 6系统中,对服务的管理主要是通过service和chkconfig命令完成

CentOS 7中则主要通过systemd中的systemctl工具来对服务进行管理


一、Systemctl命令管理服务

systemctl命令管理服务运行状态的语法格式:

systemctl start | stop | status | restart | reload 服务名称

start表示启动服务

stop表示停止服务

status表示查看服务运行状态

restart表示重启服务

reload表示重新加载服务


停止服务

systemctl stop [service-name]

重启服务

systemctl restart [service-name]

重载服务

systemctl reload [service-name]

查看服务状态

systemctl status [service-name]

服务开机启动

systemctl enable [service-name]

取消开机启动

systemctl disable [service-name]

检查开机是否启动该服务

systemctl is-enabled [service-name]

systemctl 列出所有激活的单元 list-units

systemctl list-units

列出所有活动的服务

systemctl list-units -t service

systemctl重启、关机系统

关机

systemctl poweroff

重启

systemctl reboot

休眠

systemctl hibernate

systemctl管理远程系统

systemctl status httpd -H root@192.168.0.12

-H选项:指定远程主机的用户名和密码


systemd将系统中的每个服务都看作一个服务单元(Service unit),在服务的名称后面加上“.service”作为后缀

systemctl命令对服务进行管理时,服务名称后面加不加“.service”后缀均可


案例:执行“systemctl status sshd.service”或“systemctl status sshd”命令都可以查看sshd服务的运行状态。

image.png


第二行的“Loaded: loaded (/usr/lib/systemd/system/sshd.service;enabled; vendor preset: enabled)”,表示服务已经被加载,该服务的Service unit配置文件为/usr/lib/systemd/system/sshd.service,

“enabled”表示该服务已经被设为开机自动启动,

“vendor preset: enabled”表示该服务在系统中默认被预设为开机自动启动


第三行的“Active”部分显示“active(running)”,表示服务正处于运行状态


第六行的“Main PID: 1150 (sshd)”表示该服务运行之后所产生的主进程PID为1043,进程名为sshd


二、管理服务启动状态

管理服务启动状态:

systemctl enable | disable | is-enabled 服务名 

enable 开机启动 

disable 禁止服务开机自动启动 

is-enabled 查看服务的启动状态 

# 查看系统重所有服务的开机启动状态
[root@localhost ~]# systemctl list-unit-files --type service
UNIT FILE                                     STATE   
abrt-ccpp.service                             enabled 
abrt-oops.service                             enabled 
abrt-pstoreoops.service                       disabled
abrt-vmcore.service                           enabled 
abrt-xorg.service                             enabled 
abrtd.service                                 enabled




systemctl

照顾好自己,如果尚有余力,记得保护美好的东西,不畏不惧赤诚善良。

评论

^