简述systemd的新特性及unit常见类型分析、使用systemd管理编译安装的nginx

1. systemd新特性

并行处理(同时启动)所有服务。
基于依赖关系定义的服务控制逻辑
系统状态快照
按需激活进程,只有第一次被访问时才会真正启动;

2. systemd的常见unit类型

Service unit:文件扩展名为.service,主要用于定义系统服务;
Target unit:文件扩展名为.target,主要用于模拟实现运行级别;
Device unit:文件扩展名为.device,主要用于定义内核识别的设备;
Mounu unit:文件扩展名为.mount,主要用于定义文件系统挂载点;
Socket unit:文件扩展名为.sockett,主要用于标识进程间通信用到的socket文件;
snapshot unit:文件扩展名为.snapshot,主要用于管理系统快照;
Swap unit:文件扩展名为.swap,主要用于标识swap设备;
Automount unit:文件扩展名为.automount,主要用于文件系统自动挂载点设置;
Path unit:文件扩展名为.path,主要用于定义文件系统中的文件或目录;

3. Systemd关键特性

基于socket的激活机制:socket与程序分离;
基于bus的激活机制:
基于设备device的激活机制:能监控内核输出的硬件信息,当设备插入时一旦发现就创建设备文件,再自动挂载至某挂载点,如果挂载点不存在还能自动创建;
基于path的激活机制:系统可监控某目录或文件是否存在,如果文件存在,就立即激活一个服务或进程;
    例如:某服务运行中突然崩溃,崩溃时能创建一个log或lock文件;一旦发现这个lock文件立即激活一个程序,如发送报告;
系统快照:能保存各unit的当前状态信息于持久存储设备中;因为systemd的所有管理都是通过unit实现的,回滚时使用;
向后兼容sysv init脚本:所以放在/etc/init.d/服务脚本也一样能靠systemd来启动;

4. 编译安装nginx,并使用systemd管理nginx

[Allen@centos7 ~]$ tar xf nginx-1.8.1.tar.gz
[Allen@centos7 ~]$ cd nginx-1.8.1/
[Allen@centos7 nginx-1.8.1]$ sudo ./configure --prefix=/usr/local/nginx1.8.1 --with-http_ssl_module
[Allen@centos7 nginx-1.8.1]$ make && make install


[root@centos7 ~]# cat /etc/systemd/system/nginx.service 
[Unit]
Description=nginx server daemon
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx1.8.1/sbin/nginx
ExecReload=/usr/local/nginx1.8.1/sbin/nginx -s reload
ExecStop=/usr/local/nginx1.8.1/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

[root@centos7 ~]# systemctl daemon-reload
[root@centos7 ~]# systemctl enable nginx.service
[root@centos7 ~]# systemctl start nginx.service
[root@centos7 ~]# systemctl status nginx.service
● nginx.service - nginx server daemon
   Loaded: loaded (/etc/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2018-11-02 09:59:09 CST; 4min 15s ago
     Docs: man:nginx(8)
 Main PID: 7893 (nginx)
   CGroup: /system.slice/nginx.service
           ├─7893 nginx: master process /usr/local/nginx1.8.1/sbin/nginx
           ├─7904 nginx: worker process
           ├─7905 nginx: worker process
           ├─7906 nginx: worker process
           ├─7907 nginx: worker process
           └─7908 nginx: worker process

Nov 02 09:59:09 centos7.4 systemd[1]: Starting nginx server daemon...
Nov 02 09:59:09 centos7.4 systemd[1]: Started nginx server daemon.
Nov 02 09:59:43 centos7.4 systemd[1]: Reloaded nginx server daemon.

猜你喜欢

转载自www.cnblogs.com/jzbgltb/p/9895224.html