[root@c7-server ~]# systemctl list-unit-files UNIT FILE STATE proc-sys-fs-binfmt_misc.automount static ... tmp.mount disabled ... cups.path enabled ... sssd-autofs.service indirect ...
显示服务状态[root@c7-server ~]# systemctl status httpd.service ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: inactive (dead) Docs: man:httpd(8) man:apachectl(8) [root@c7-server ~]# systemctl start httpd.service [root@c7-server ~]# systemctl status httpd.service ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2019-11-22 11:49:33 CST; 1s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 3193 (httpd) Status: "Processing requests..." Tasks: 6 CGroup: /system.slice/httpd.service ├─3193 /usr/sbin/httpd -DFOREGROUND ├─3195 /usr/sbin/httpd -DFOREGROUND ├─3196 /usr/sbin/httpd -DFOREGROUND ├─3197 /usr/sbin/httpd -DFOREGROUND ├─3199 /usr/sbin/httpd -DFOREGROUND └─3201 /usr/sbin/httpd -DFOREGROUND Nov 22 11:49:32 c7-server systemd[1]: Starting The Apache HTTP Server... Nov 22 11:49:33 c7-server systemd[1]: Started The Apache HTTP Server.
服务处于启动或停止的状态时,有不同的状态信息显示。使用root用户执行此命令,还可以显示日志信息。
如果仅希望查看服务是否启动,是否开机启动的话,可使用如下命令。
[root@c7-server ~]# systemctl is-active httpd.service active [root@c7-server ~]# systemctl is-enabled httpd.service disabled
启动、停止和重启等~]# systemctl start httpd.service ~]# systemctl stop httpd.service ~]# systemctl restart httpd.service
如果服务原本是停止的状态,则执行restart会启用该服务。如果我们希望只有当服务当前是启动的状态才重启的话,那么应该执行try-restart。
~]# systemctl try-restart httpd.service
某些服务支持在不打断运行状态的情况下重载服务从而读取配置文件。
~]# systemctl reload httpd.service
如果服务不支持重载的话,服务会忽略systemd的reload操作。为了方便,确保修改的配置文件一定会生效,可以执行如下2个子命令。
reload-or-restart:尝试reload,如果服务不支持就restart。
reload-or-try-restart:尝试reload,如果服务不支持就try-restart。如果服务本身没启动,那么再执行了此命令后应该也不会启动,需要手工启动。启动时,服务就会去读取配置文件了。
~]# systemctl reload-or-restart httpd.service ~]# systemctl reload-or-try-restart httpd.service
开机启动~]# systemctl enable httpd.service ~]# systemctl disable httpd.service
当我们设置开机启动的时候,systemd会读取服务的配置文件(/usr/lib/systemd/system/httpd.service)中的[Install]部分。
[Install] WantedBy=multi-user.target
根据此部分的内容创建字符链接。
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
如果服务本身是开机启动的,那么再执行一次enable的话并不会创建字符链接文件。想确保其一定创建的话,可使用reenable。
~]# systemctl reenable httpd.service 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.
关闭开机启动,就是删除字符链接了。
~]# systemctl disable httpd.service Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
systemd支持mask操作,如果一个服务被mask了,那么它无法被手动启动或者被其他服务所启动,也无法被设置为开机启动。