systemd

systemd

Linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 bootloader 载入内核,进行内核初始化。内核初始化的最后一步就是启动 pid 为 1 的 init 进程。这个进程是系统的第一个进程。它负责产生其他所有用户进程。
大多数 Linux 发行版的 init 系统是和 System V 相兼容的,被称为 sysvinit。这是人们最熟悉的 init 系统。Ubuntu 和 RHEL 采用 upstart 替代了传统的 sysvinit。而 Fedora 从版本 15 开始使用了一个被称为 systemd 的新 init 系统。

systemd 的使用

linux系统启动时,systemd会执行/etc/systemd/system目录里面的配置文件。对于支持systemd的软件,在安装时,会自动在 /usr/lib/systemd/system 目录添加一个配置文件,所以想设置软件为开机自启,就可以执行以下命令:

$ sudo systemctl enable application.service

以上命令相当于在/etc/systemd/system目录中添加一个符号链接,指向/usr/lib/systemd/system。

常用命令

 $ sudo systemctl start application.service #启动服务
 $ sudo systemctl stop application.service #停止服务
 $ sudo systemctl restart application.service #重启服务
 $ sudo systemctl enable application.service #设置开机自启
 $ sudo systemctl disable application.service #取消开机自启
 $ systemctl status application.service #查看服务的状态
 $ systemctl is-active application.service #查看服务是否有效
 $ systemctl is-enabled application.service #查看服务是否设置开机自启
 $ systemctl is-failed application.service #查看服务是否开启失败
 $ systemctl list-units #列出全部服务

其中查看服务状态:

$ sudo systemctl status httpd

httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since 金 2014-12-05 12:18:22 JST; 7min ago
 Main PID: 4349 (httpd)
   Status: "Total requests: 1; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─4349 /usr/sbin/httpd -DFOREGROUND
           ├─4350 /usr/sbin/httpd -DFOREGROUND
           ├─4351 /usr/sbin/httpd -DFOREGROUND
           ├─4352 /usr/sbin/httpd -DFOREGROUND
           ├─4353 /usr/sbin/httpd -DFOREGROUND
           └─4354 /usr/sbin/httpd -DFOREGROUND

12月 05 12:18:22 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
12月 05 12:18:22 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
12月 05 12:22:40 localhost.localdomain systemd[1]: Started The Apache HTTP Server.

输出结果的含义如下:

- Loaded 行:配置文件的位置,是否设为开机启动
- Active 行: 表示正在运行
- Main PID 行:主进程ID
- Status 行:由应用本身(这里是httpd)提供的软件当前状态
- CGroup 快:应用的所有子进程
- 日志块:应用的日志

参考博客:
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html
http://www.ibm.com/developerworks/cn/linux/1407_liuming_init3/
https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

猜你喜欢

转载自blog.csdn.net/ganzheyu/article/details/56335419