systemd
Table of Contents
1. systemd
systemd 可以用来管理服务(daemon 进程),它的主命令为 systemctl
。
systemd 会在目录(/lib/systemd/system 和 /usr/local/lib/systemd/system 和 /etc/systemd/system/)中查找服务单元的配置文件。如果软件支持用 systemd 管理,那么在安装的时候,往往会添加自己的配置文件到前面的某个目录中。
2. systemctl 实例
2.1. 基本操作(start/stop/restart)
通过子命令 start/stop/restart 可以对某个服务进行管理,如操作 MySQL:
$ sudo systemctl start mysql # 启动MySQL $ sudo systemctl stop mysql # 停止MySQL $ sudo systemctl restart mysql # 重启 MySQL $ sudo systemctl status mysql # 查看MySQL状态
下面是 sudo systemctl status ssh
的输出例子:
$ sudo systemctl status ssh ● ssh.service - OpenBSD Secure Shell server Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2018-02-04 11:33:59 UTC; 4 months 24 days ago Main PID: 730 (sshd) Tasks: 1 (limit: 488) CGroup: /system.slice/ssh.service └─730 /usr/sbin/sshd -D
2.2. 列出服务单元配置文件(systemctl list-unit-files)
使用 systemctl list-unit-files
可以列出系统中所有服务单元配置文件,配合 grep 可查找想要服务单元配置文件,如:
$ systemctl list-unit-files | grep mysql mysql.service enabled
2.3. 查看系统中运行的服务
使用 systemctl |grep running
可以查看系统中运行的服务,如:
$ systemctl |grep running session-1.scope loaded active running Session 1 of user ec2-user acpid.service loaded active running ACPI Event Daemon atd.service loaded active running Job spooling tools auditd.service loaded active running Security Auditing Service chronyd.service loaded active running NTP client/server crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus getty@tty1.service loaded active running Getty on tty1 gssproxy.service loaded active running GSSAPI Proxy Daemon irqbalance.service loaded active running irqbalance daemon libstoragemgmt.service loaded active running libstoragemgmt plug-in server daemon lvm2-lvmetad.service loaded active running LVM2 metadata daemon mariadb.service loaded active running MariaDB database server network.service loaded active running LSB: Bring up/down networking nginx.service loaded active running The nginx HTTP and reverse proxy server php-fpm.service loaded active running The PHP FastCGI Process Manager ......
2.4. 开机启动(systemctl enable)
使用 systemctl enable xxx
可以使服务在开机时自动启动。如:
$ sudo systemctl enable mysql
执行上面的命令相当于在目录/etc/systemd/system/中添加一个符号链接,指向/lib/systemd/system/里的 mysql.service 文件。开机时,systemd 只执行/etc/systemd/system 目录里面的配置文件。
3. journalctl 实例
journalctl
是 systemd 日志管理相关命令。
3.1. 查看指定某服务的日志
通过选项 -u
或者 --unit
可以指定服务的日志:
$ sudo journalctl -u ssh.service # 查看 ssh 相关日志 $ sudo journalctl -f -u ssh.service # 查看最新的 ssh 相关日志,等待新日志不退出 $ sudo journalctl -u myapp # 查看 myapp 相关日志
如果某行日志太长,超过终端的部分会被截断。如果想显示完整的日志,可以把日志通过管道发送给 less,如:
$ sudo journalctl -u ssh.service | less # 日志不会被截断
4. 编写 Unit File
下面是一个 Unit File 的例子(假设文件名为 myapp.service):
[Unit] Description=My file svr app [Service] ExecStart=/usr/local/bin/node /home/ubuntu/file-svr/app.js Restart=always # Restart service after 5 seconds if node service crashes RestartSec=5 User=nobody Group=nogroup # Group Note: Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody' Environment=PATH=/usr/bin:/usr/local/bin Environment=NODE_ENV=production WorkingDirectory=/home/ubuntu/file-svr [Install] WantedBy=multi-user.target
把它复制到目录 /etc/systemd/system 中,执行下面命令可以启动它:
$ sudo systemctl start myapp # 启动 myapp $ sudo journalctl -u myapp # 查看 myapp 日志