Abstract: Systemd 有争议,但是Fedora,SFedora,Suse,Rhel 都逐渐投向了systemd。都逐渐投向了systemd。我们将会学习使用systemd 控制系统启动systemd。我们将会学习使用systemd 控制系统启动,管理服务,查看日志。我们将会学习使用systemd 控制系统启动,管理服务,查看日志 我们将会学习使用systemd 控制系统启动,管理服务,查看日志我们将会学习使用systemd 控制系统启动,管理服务,查看日志systemd 控制系统启动,管理服务,查看日志控制系统启动,管理服务,查看日志
“SystemD 入门”
Systemd 简介:
systemd 是由LennartPoettering带头开发,并在LGPL2.1及其后续版本许可证下开源发布。其开发目标是提供更优秀的框架以表示系统服务间的依赖关系,并依此实现系统初始化时服务的并行启动,同时达到降低Shell的系统开销的效果,最终代替现在常用的SystemV与BSD风格init程序。(来自维基百科)
Systemd 存在争议,但是Fedora,Suse,Rhel 都逐渐投向了systemd,本文将在Rhel7 上使用systemd,带领读者学习使用systemd 控制系统启动,查看日志,管理服务。
系统启动:
我们首先来回忆下过去,/sbin/init 作为PID 为1的进程首先启动,然后fork(clone)其他的进程,按照/etc/inittab里面的设置选择runlevel, 并从/etc/rc*/etc/init.d/ 下面选择相应的启动脚本来启动服务。而现在PID 为1的进程变成了sytemd ,(PID0是内核进程)在我的机器上则显示为:
[root@22062~]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 Jan14 ? 00:01:51 /usr/lib/systemd/systemd--system --deserialize 17
由systemd来spawn其他的进程。
而Runlevel 则变成了 Target:
graphical.target=5
multiuser.target=3
下面是一些常用的的命令:
查看defaulttarget: systemctl get-default
修改defaulttarget: systemctl set-default []
实时修改:systemctlisolate []
查看启动时间:systemd-analyze
查看单项任务启动的时间开销: systemd-analyzeblame
查看任务之间的依赖关系: systemctl list-dependencies
(注:/etc/inittab 不再使用,为向上兼容保留了runlevel5.target,runlevel3.target)
管理服务:
在以前,默认的启动脚本都放在/etc/init.d/etc/rc*下面。
Systemd 使用Unitfile 来控制服务。
控制文件:/usr/lib/systemd/system
控制文件:/etc/systemd/system
运行时数据:/run/systemd
(注:/etc 目录下的文件具有更高优先级)
我在这台机器上装了HTTPD,我们来看一下HTTPD服务的状态。
[root@22062~]# systemctl status httpd
httpd.service- The Apache HTTP Server
Loaded:loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active:active (running) since Wed 2015-01-21 03:46:34 EST; 4 days ago
Process:18544 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited,status=0/SUCCESS)
MainPID: 10701 (httpd)
Status:"Total requests: 0; Current requests/sec: 0; Current traffic:0 B/sec"
CGroup:/system.slice/httpd.service
├─10701/usr/sbin/httpd -DFOREGROUND
├─18556/usr/sbin/httpd -DFOREGROUND
├─18557/usr/sbin/httpd -DFOREGROUND
├─18558/usr/sbin/httpd -DFOREGROUND
├─18559/usr/sbin/httpd -DFOREGROUND
└─18560/usr/sbin/httpd -DFOREGROUND
SystemD默认我们这里输入都是Service,所以
systemctlstatus httpd 等同于 systemctlstatus httpd.service , Rhel 7 会把servicehttpd status 重定向到 systemctlstatus httpd.
关闭httpd服务:systemctlstop httpd
systemctldisable httpd ( 等同于以前的chkconfighttpd off)
重启/重新加载:systemctlrestart httpd / systemctlreload httpd
查看httpd 服务是否是自启动的:systemctlis-enable httpd
查看httpd 是否启动: systemctl is-active httpd
查看httpd 的状态: systemctlstatus httpd
查看所有服务的状态:systemctl
以树状方式查看服务:systemd-cgls
systemd-cgls 给出的树状结构非常具体,可以看到Cgroup ,服务,进程之间的层级)
在这里,我省略了一些输出。 (注:slice 并不是服务的容器)
└─system.slice
├─httpd.service
│ ├─10701/usr/sbin/httpd -DFOREGROUND
│ ├─18556/usr/sbin/httpd -DFOREGROUND
│ ├─18557/usr/sbin/httpd -DFOREGROUND
│ ├─18558/usr/sbin/httpd -DFOREGROUND
│ ├─18559/usr/sbin/httpd -DFOREGROUND
│ └─18560/usr/sbin/httpd -DFOREGROUND
控制文件:
下面我们来看httpd的控制文件,这个文件是软件安装时自动生成的,我们也可以在/etc/systemd/system/再生成一个httpd.service 文件,那么/lib/systemd/system/下的同名文件内容将会被覆盖。
[root@22062~]# cat /lib/systemd/system/httpd.service
[Unit]
Description=TheApache HTTP Server
After=network.targetremote-fs.target nss-lookup.target
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd$OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd$OPTIONS -k graceful
ExecStop=/bin/kill-WINCH ${MAINPID}
#We want systemd to give httpd some time to finish gracefully, butstill want
#it to kill httpd after TimeoutStopSec if something went wrong duringthe
#graceful stop. Normally, Systemd sends SIGTERM signal right after the
#ExecStop, which would kill httpd. We are sending useless SIGCONT hereto give
#httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
停止服务:
systemctlstop httpd 或者 systemctlkill httpd
这两个命令都能停止服务,但实现过程不同。
kill 是直接发送SIGTERM 给这个Cgroup,而stop 则是按照httpd 这个Unit的配置,ExecStop=/bin/kill-WINCH ${MAINPID}是stop 真正调用的命令。
查看日志:
在Systemd 内部它提供了一个日志,该日志会记录syslog,Kernellog,boot messages.
你可以把他当作一个syslog/rsyslog 的替代品。
简单的输入journalctl 就能查看日志。日志保存在 /run/systemd/journal/
Systemd 小结:
本文仅仅是systemd入门知识,在Rhel7上做了些简单的实验。
Systemd 的引入是很大的变化,体现在并发启动,cgroup管理服务,对于资源的管理, 对于容器的管理,systemd 有很多内容值得挖掘。
参考资料:
IBMdeveloperWorks 上的文章:http://www.ibm.com/developerworks/cn/linux/1407_liuming_init3/index.html
LennartPoettering的blog:http://0pointer.net/blog/archives.html (systemd开发者的网站,上面有一系列的systemd的文章,非常推荐)
RHEL7:How to get started withSystemd:http://www.certdepot.net/rhel7-get-started-systemd/
demystifyingsystemd(需翻墙): http://rhsummit.files.wordpress.com/2014/04/summit_demystifying_systemd1.pdf
Sytemd : http://www.freedesktop.org/wiki/Software/systemd/