centos7——systemd介绍

引文

假设我们有这样一个需求:
有一个程序,运行环境为CentOS 7系统,需要在开机时启动它,当程序进程crash之后,守护进程立即拉起进程。

解决方案:
使用CentOS 7中的init进程systemd

systemd介绍

Linux一直以来采用init进程。例如下面的命令用来启动服务:

  • $ /etc/init.d/ apache2 start
  • $ service apache2 start(和init.d一样)

但是init有两个缺点:

  • 启动时间长。Init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。(这也是CentOS5的主要特征)
  • 启动脚本复杂。Init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这使得脚本变得很长而且复杂。

Centos 5 Sys init 是启动速度最慢的,串行启动过程,无论进程相互之间有无依赖关系。
Centos6 Upstart init 相对启动速度快一点有所改进。有依赖的进程之间依次启动而其他与之没有依赖关系的则并行同步启动。
Centos7 systemd 与以上都不同。所有进程无论有无依赖关系则都是并行启动(当然很多时候进程没有真正启动而是只有一个信号或者说是标记而已,在真正利用的时候才会真正启动)

systemd为了解决上文的问题而诞生。它的目标是,为系统的启动和管理提供一套完整的解决方案。根据linux惯例,字母d是守护进程(daemon) 的缩写。Systemd名字的含义就是 守护整个系统。Centos 7里systemd代替了init,成为了系统的第一个进程。PID为1.其他所有的进程都是它的子进程。

systemd 是 Linux 下的一款系统和服务管理器,兼容 SysV 和 LSB 的启动脚本。systemd 的特性有:支持并行化任务;同时采用 socket 式与 D-Bus 总线式激活服务;按需启动守护进程(daemon);利用 Linux 的 cgroups 监视进程;支持快照和系统恢复;维护挂载点和自动挂载点;各服务间基于依赖关系进行精密控制。

systemctl命令

systemctl命令是systemd引入的,它实际上将 service 和 chkconfig 这两个命令组合到一起。用service、chkconfig命令来管理服务的时候,是在/etc/init.d/目录中创建一个脚本,来管理服务的启动和停止;在systemctl中也是类似的,目录为/usr/lib/systemd/system/。(/lib 目录是/usr/lib目录的软连接)
1)systemctl命令介绍:

  • systemctl start my.service  #启动my.service服务
  • systemctl stop my.service  #关闭my.service服务
  • systemctl restart my.service  #重启my.service服务
  • systemctl status my.service  #查看my.service服务窗台
  • systemctl enable my.service  #开启开机自启动my.service服务
  • systemctl disable my.service  #关闭开机自启动my.service服务
  • systemctl is-enabled my.service;echo $?  #查看服务是否开机启动
  • systemctl list-unit-files  #查看已启动的服务列表

同样,systemctl执行命令后面的参数名(my.service)就是/lib/systemd/system/下脚本名。

2)systemctl 服务文件介绍:

systemctl的服务文件位于/lib/systemd/system/下,格式如下:

[Unit] 
Description=nginx - high performance web server 
After=network.target remote-fs.target nss-lookup.target 

[Service] 
Type=forking
PIDFile=/var/run/nginx.pid 
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload 
ExecStop=/usr/local/nginx/sbin/nginx -s stop 

[Install] 
WantedBy=multi-user.target

[Unit]:服务的说明

  • Description:描述服务
  • After:依赖,当依赖的服务启动之后再启动自定义的服务

[Service]服务运行参数的设置

  • Type=forking:后台运行的形式,使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程;
  • ExecStart为服务的具体运行命令
  • ExecReload为重启命令
  • ExecStop为停止命令
  • PrivateTmp=True表示给服务分配独立的临时空间

[Install]服务安装的相关设置:

  • WantedBy,multi-user.target表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行;除此之外还有:default.target.wants、sockets.target.wants、sysinit.target.wants、system-update.target.wants、basic.target.wants、getty.target.wants...

注:执行systemctl enable XXX.service 设置XXX为开机自启动时,系统会把/lib/systemd/system/下XXX.service脚本创立一个软连接,到/etc/systemd/system/wantedBy/ 下,其中wantedBy就是这里指定的,如:multi-user.target。

demo

1)创建脚本:

$ cd ~
$ vim test_init_watch.sh
#!/bin/bash

# test_init_watch.sh
while [ 1 ]
do
    echo `date` >> /home/cooper/log.txt
    sleep 1
done
$ chmod 777 test_init_watch.sh

//创建软链接/home/cooper/test_init_watch.sh =>/usr/sbin/sysd-test
$ sudo cd /usr/sbin
$ ln -sf /home/cooper/test_init_watch.sh sysd-test

2)创建服务文件:

注:systemd服务文件放置目录有系统和用户区分;系统(/usr/lib/systemd/system/)、用户(/usr/lib/systemd/user/)。在开机中没有登陆情况下就能运行的程序,应该放置在系统目录中;反之则放在用户目录中
//为了测试方便,把服务文件放在/usr/lib/systemd/system中(借鉴sshd.service):

$ cd /usr/lib/systemd/system/
$ cp sshd.service sysd-test.service     
$ vim sysd-test.service

[Unit]
Description=sysd server daemon
Documentation=no
After=no
Wants=no

[Service]
EnvironmentFile=no
ExecStart=/usr/sbin/sysd-test
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=1s

[Install]
WantedBy=multi-user.target graphic.target      #相当于runlevel:2345

 激活开机启动,启用runlevel的2345级别

$ systemctl enable sysd-test.service
//等价于创建软链接(下面的命令不用执行)
$ cd /etc/systemd/system/multi-user.target.wants
$ ln -sf /usr/lib/systemd/system/sysd-test.service sysd-test.service
$ cd /usr/lib/systemd/system/graphical.target.wants/
$ ln -sf /usr/lib/systemd/system/sysd-test.service sysd-test.service

设置守护:

systemctl [start|stop|status|reload|kill] sysd-test.service

例如:
$ systemctl start sysd-test.service
$ pgrep sysd-test
$ pkill -9 sysd-test
$ pgrep sysd-test
//会发现两次pgrep进程id不一样了,表示系统自动拉起了sysd-test

参考:

https://blog.csdn.net/hellocooper/article/details/53771125

发布了800 篇原创文章 · 获赞 460 · 访问量 436万+

猜你喜欢

转载自blog.csdn.net/liuxiao723846/article/details/103685486
今日推荐