基本用法
[[查看全部服务]]
systemctl list-unit-files --type service
[[查看服务状态]]
systemctl status name.service
[[启动服务]]
systemctl start name.service
[[停止服务]]
systemctl stop name.service
[[重启服务]]
systemctl restart name.service
[[增加开机启动]]
systemctl enable name.service
[[删除开机启动]]
systemctl disable name.service
[[重新加载服务文件]]
systemctl daemon-reload
任务的几种状态
未开启,正在启动,已启动
Active: inactive (dead)
Active: activating (auto-restart) (Result: exit-code) since 五 2019-11-15 13:46:29 CST; 4s ago
Active: active (running) since 五 2019-11-15 13:50:39 CST; 2h 35min ago
Status
[root@drools-service ~]# systemctl status postgresql-11 -l
● postgresql-11.service - PostgreSQL 11 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-11.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2020-04-14 13:13:21 CST; 1 months 18 days ago
Docs: [https://www.postgresql.org/docs/11/static/](https://www.postgresql.org/docs/11/static/)
Main PID: 11627 (postmaster)
CGroup: /system.slice/postgresql-11.service
├─ 533 postgres: postgres fss400 175.31.201.131(63442) idle
├─ 534 postgres: postgres fss400 175.31.201.131(63444) idle
Apr 14 13:13:20 drools-service systemd[1]: Starting PostgreSQL 11 database server...
Apr 14 13:13:20 drools-service postmaster[11627]: 2020-04-14 13:13:20.991 CST [11627] LOG: listening on IPv4 address "0.0.0.0", port 5432
Apr 14 13:13:20 drools-service postmaster[11627]: 2020-04-14 13:13:20.991 CST [11627] LOG: listening on IPv6 address "::", port 5432
Apr 14 13:13:20 drools-service postmaster[11627]: 2020-04-14 13:13:20.993 CST [11627] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Apr 14 13:13:20 drools-service postmaster[11627]: 2020-04-14 13:13:20.996 CST [11627] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
Apr 14 13:13:21 drools-service postmaster[11627]: 2020-04-14 13:13:21.029 CST [11627] LOG: redirecting log output to logging collector process
Apr 14 13:13:21 drools-service postmaster[11627]: 2020-04-14 13:13:21.029 CST [11627] HINT: Future log output will appear in directory "log".
Apr 14 13:13:21 drools-service systemd[1]: Started PostgreSQL 11 database server.
Loaded:配置文件的位置,是否设置为开机启动,厂商预设的是开机不启动。 Active:表示正在运行,以及运行的多长时间。 Docs:官方文档位置。 Main PID:主进程ID。 CGroup:所有的子进程 日志:最下面的是一些日志。
Systemd
配置文件
在/usr/lib/systemd/system目录下新建 tomcat9.service 文件,添加如下配置信息。
vim /usr/lib/systemd/system/tomcat9.service
[Unit]
Description=Apache Tomcat 9
After=syslog.target network.target
[Service]
Type=forking
# 启动程序所需要的环境信息
Environment=JAVA_HOME=/opt/jdk1.8.0_251
Environment=CATALINA_PID=/opt/apache-tomcat-9.0.36/tomcat.pid
Environment=CATALINA_HOME=/opt/apache-tomcat-9.0.36
Environment=CATALINA_BASE=/opt/apache-tomcat-9.0.36
ExecStart=/opt/apache-tomcat-9.0.36/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
# 注意启动程序的用户和所属用户组
User=root
Group=root
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
字段解释
Unit 中的 Description 字段是当前服务的基本描述。 Unit 中的 After 字段是当前服用所依赖的前置服务。 Service 中的 Type 字段表示启动类型。 simple(默认值):ExecStart字段启动的进程为主进程 forking:ExecStart字段将以fork()方式启动,此时父进程将会退出,子进程将成为主进程 oneshot:类似于simple,但只执行一次,Systemd 会等它执行完,才启动其他服务 dbus:类似于simple,但会等待 D-Bus 信号后启动 notify:类似于simple,启动结束后会发出通知信号,然后 Systemd 再启动其他服务 idle:类似于simple,但是要等到其他任务都执行完,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混合* Service 中的 ExecStart 字段定义启动时执行的命令。
Install 配置
定义如何安装这个配置文件。 Install 中的 WantedBy 字段表示该服务所在的 Target,Target 是一个服务组。 WantedBy=multi-user.target 指的是默认 Target 也是最常用的 Target,这个组里面的服务都将开机启动。 systemctl get-default multi-user.target 执行systemctl enable sshd.service命令时,sshd.service的一个符号链接,就会放在/usr/lib/systemd/system/multi-user.target.wants目录中。
Reference
https://www.freedesktop.org/software/systemd/man/bootup.html#System Manager Bootup