centos7 中 systemd systemctl管理服务的命令

背景: centos7 和 6的重大改变

对于系统管理员而言,系统的服务管理是一件很日常和很重要的工作,而7在6的基础上有了很大的改变,就连命令都完全不一样了。所以要拥抱变化,学习7是如何进行服务的管理和控制的。



system和systemctl的初探

Systemctl是一个systemd工具,主要负责控制systemd系统和服务管理器。

Systemd是一个系统管理守护进程、工具和库的集合,用于取代System V初始进程。Systemd的功能是用于集中管理和配置类UNIX系统。

在Linux生态系统中,Systemd被部署到了大多数的标准Linux发行版中,只有为数不多的几个发行版尚未部署。Systemd通常是所有其它守护进程的父进程,但并非总是如此。


【第一步】

查看systemd的版本

# systemctl --version

systemd 219

+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN


查看进程是否存在

ps -ef | grep [s]ystemd


注意:systemd是作为父进程(pid=1)运行的


分析systemd的启动进程

# systemd-analyze

Startup finished in 639ms (kernel) + 931ms (initrd) + 12.432s (userspace) = 14.003s


分析各个进程启动发费的时间

#systemd-analyze blame


分析启动时候的关键链

systemd-analyze critical-chain


重要:Systemctl接受服务(.service),挂载点(.mount),套接口(.socket)和设备(.device)作为单元。



列出所有服务可用单元

systemctl list-unit-files


(200多个)


列出所有运行中的单元

systemctl list-units


列出失败的单元?

systemctl --failed


列出某个单元是否启动

# systemctl is-enabled crond.service

enabled

或者

# systemctl is-enabled crond

enabled


检查某个单元或服务是否运行



# systemctl is-active crond 

active

或者

# systemctl status crond   

这个信息更详细


############################# 控制服务  ############################

列出所有服务(包括启用的和禁用的)

systemctl list-unit-files  --type=service

(120+)


以httpd为例

yum install httpd

会生成以下文件

/usr/lib/systemd/system/httpd.service 



Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态

[root@Centos7-node2 ~]# systemctl status httpd

● 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@Centos7-node2 ~]# systemctl start httpd 

[root@Centos7-node2 ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server

   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

   Active: active (running) since Sun 2016-10-23 00:48:43 CST; 3s ago

     Docs: man:httpd(8)

           man:apachectl(8)

 Main PID: 26811 (httpd)

   Status: "Processing requests..."

   CGroup: /system.slice/httpd.service

           ├─26811 /usr/sbin/httpd -DFOREGROUND

           ├─26812 /usr/sbin/httpd -DFOREGROUND

           ├─26813 /usr/sbin/httpd -DFOREGROUND

           ├─26814 /usr/sbin/httpd -DFOREGROUND

           ├─26815 /usr/sbin/httpd -DFOREGROUND

           └─26816 /usr/sbin/httpd -DFOREGROUND


Oct 23 00:48:17 Centos7-node2 systemd[1]: Starting The Apache HTTP Server...

Oct 23 00:48:33 Centos7-node2 httpd[26811]: AH00558: httpd: Could not reliably determine the server's fully qua...ssage

Oct 23 00:48:43 Centos7-node2 systemd[1]: Started The Apache HTTP Server.

Hint: Some lines were ellipsized, use -l to show in full.

[root@Centos7-node2 ~]# systemctl reload httpd

[root@Centos7-node2 ~]# systemctl stop httpd  




注意:

Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)


在loader中第一个disabled表示系统启动是否自启动,为非自动。 第二个不知道干嘛


Active: active (running) since Sun 2016-10-23 00:48:43 CST; 3s ago


Active 表示服务是不是在运行



如何激活服务并在启动时启用或禁用服务(即系统启动时自动启动服务)

[root@Centos7-node2 ~]# systemctl enable httpd

Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.


禁用

systemctl disabled httpd

[root@Centos7-node2 ~]# systemctl disable httpd 

Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.



使用systemctl命令杀死服务

[root@Centos7-node2 ~]# systemctl kill httpd

[root@Centos7-node2 ~]# systemctl status httpd

● httpd.service - The Apache HTTP Server

   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)

   Active: failed (Result: exit-code) since Sun 2016-10-23 00:56:59 CST; 1s ago

     Docs: man:httpd(8)

           man:apachectl(8)

  Process: 26901 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)

 Main PID: 26869 (code=exited, status=0/SUCCESS)

   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"


Oct 23 00:51:38 Centos7-node2 systemd[1]: Starting The Apache HTTP Server...

Oct 23 00:51:53 Centos7-node2 httpd[26869]: AH00557: httpd: apr_sockaddr_info_get() failed for Centos7-node2

Oct 23 00:51:53 Centos7-node2 httpd[26869]: AH00558: httpd: Could not reliably determine the server's fully qua...ssage

Oct 23 00:52:03 Centos7-node2 systemd[1]: Started The Apache HTTP Server.

Oct 23 00:56:59 Centos7-node2 kill[26901]: kill: cannot find process ""

Oct 23 00:56:59 Centos7-node2 systemd[1]: httpd.service: control process exited, code=exited status=1

Oct 23 00:56:59 Centos7-node2 systemd[1]: Unit httpd.service entered failed state.

Oct 23 00:56:59 Centos7-node2 systemd[1]: httpd.service failed.

Hint: Some lines were ellipsized, use -l to show in full.



注意:Active: failed (Result: exit-code) since Sun 2016-10-23 00:56:59 CST; 1s ago

这个的failed表示的是kill掉的?




##########################  使用Systemctl控制并管理挂载点 #################3

[root@Centos7-node2 ~]# systemctl list-unit-files --type=mount

UNIT FILE                     STATE   

dev-hugepages.mount           static  

dev-mqueue.mount              static  

proc-sys-fs-binfmt_misc.mount static  

sys-fs-fuse-connections.mount static  

sys-kernel-config.mount       static  

sys-kernel-debug.mount        static  

tmp.mount                     disabled



挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态

systemctl start tmp.mount

systemctl status tmp.mount

systemctl reload tmp.mount


在启动时激活、启用或禁用挂载点(系统启动时自动挂载)


# systemctl is-active tmp.mount

# systemctl enable tmp.mount

# systemctl disable tmp.mount


在Linux中屏蔽(让它不能启用)或可见挂载点


# systemctl mask tmp.mount

ln -s '/dev/null''/etc/systemd/system/tmp.mount'

# systemctl unmask tmp.mount

rm '/etc/systemd/system/tmp.mount'

 



####################  控制系统运行等级  ##################


 


启动系统救援模式


# systemctl rescue

Broadcast message from root@tecmint on pts/0(Wed2015-04-2911:31:18 IST):

The system is going down to rescue mode NOW!

 


进入紧急模式


# systemctl emergency   

Welcome to emergency mode!After logging in, type "journalctl -xb" to view

system logs,"systemctl reboot" to reboot,"systemctl default" to try again

to boot intodefault mode.

 

列出当前使用的运行等级


注意:init 1也是可以使用的

# systemctl get-default

multi-user.target

 

注意:who -r 也是可以查看的


启动运行等级5,即图形模式


# systemctl isolate runlevel5.target

# systemctl isolate graphical.target

 


启动运行等级3,即多用户模式(命令行)


# systemctl isolate runlevel3.target

# systemctl isolate multiuser.target

 


设置多用户模式或图形模式为默认运行等级


# systemctl set-default runlevel3.target

# systemctl set-default runlevel5.target

 


重启、停止、挂起、休眠系统或使系统进入混合睡眠


# systemctl reboot             

# systemctl halt

# systemctl suspend

# systemctl hibernate

# systemctl hybrid-sleep

对于不知运行等级为何物的人,说明如下。


Runlevel 0 : 关闭系统

Runlevel 1 : 救援?维护模式

Runlevel 3 : 多用户,无图形系统

Runlevel 4 : 多用户,无图形系统

Runlevel 5 : 多用户,图形化系统

Runlevel 6 : 关闭并重启机器



注意:在centos7 中仍然可以使用init 0 关机 init 6 启动。   

本文转自残剑博客51CTO博客,原文链接http://blog.51cto.com/cuidehua/1864579如需转载请自行联系原作者


cuizhiliang

上一篇:磁盘空间满更换磁盘同步数据


下一篇:数据库管理——Powershell——使用Powershell脚本找出消耗最多磁盘空间的文件