如何把应用服务手动注册到Systemd
# 如何把应用服务注册到Systemd
郭群 于 2020年07月14日

# 一、引言
历史上,Linux 的启动一直采用init进程。 下面的命令用来启动服务。
$ sudo /etc/init.d/apache2 start
或者
$ service apache2 start
这种方法有两个缺点:
- 一是启动时间长。init进程是串行启动,只有前一个进程启动完,才会启动下一个进程。
- 二是启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。
Systemd 就是为了解决这些问题而诞生的。它的设计目标是,为系统的启动和管理提供一套完整的解决方案。 根据 Linux 惯例,字母d是守护进程(daemon)的缩写。 Systemd 这个名字的含义,就是它要守护整个系统。
当前较新一些的Linux发行版,比如Ubuntu16以上、CentOS7以上、RedHat7以上的操作系统,其系统服务控制命令都开始使用Systemd,这种统一是件好事。
# 二、简介
Systemd 并不是一个命令,而是一组命令,涉及到系统管理的方方面面。 主要有如下一些命令:
- systemctl
systemctl是 Systemd 的主命令,用于管理系统
# 重启系统
$ sudo systemctl reboot
# 关闭系统,切断电源
$ sudo systemctl poweroff
# CPU停止工作
$ sudo systemctl halt
# 暂停系统
$ sudo systemctl suspend
# 让系统进入冬眠状态
$ sudo systemctl hibernate
# 让系统进入交互式休眠状态
$ sudo systemctl hybrid-sleep
# 启动进入救援状态(单用户状态)
$ sudo systemctl rescue
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- systemd-analyze
systemd-analyze命令用于查看启动耗时
# 查看启动耗时
$ systemd-analyze
# 查看每个服务的启动耗时
$ systemd-analyze blame
# 显示瀑布状的启动过程流
$ systemd-analyze critical-chain
# 显示指定服务的启动流
$ systemd-analyze critical-chain atd.service
2
3
4
5
6
7
8
9
10
11
- hostnamectl
hostnamectl命令用于查看当前主机的信息
# 显示当前主机的信息
$ hostnamectl
# 设置主机名。
$ sudo hostnamectl set-hostname rhel7
2
3
4
5
- localectl
localectl命令用于查看本地化设置
# 查看本地化设置
$ localectl
# 设置本地化参数。
$ sudo localectl set-locale LANG=en_GB.utf8
$ sudo localectl set-keymap en_GB
2
3
4
5
6
- timedatectl
timedatectl命令用于查看当前时区设置
# 查看当前时区设置
$ timedatectl
# 显示所有可用的时区
$ timedatectl list-timezones
# 设置当前时区
$ sudo timedatectl set-timezone America/New_York
$ sudo timedatectl set-time YYYY-MM-DD
$ sudo timedatectl set-time HH:MM:SS
2
3
4
5
6
7
8
9
10
- loginctl
loginctl命令用于查看当前登录的用户
# 列出当前session
$ loginctl list-sessions
# 列出当前登录用户
$ loginctl list-users
# 列出显示指定用户的信息
$ loginctl show-user ruanyf
2
3
4
5
6
7
8
# 三、日常工作高频使用的的systemctl命令
用于启动和停止 Unit(主要是 service)
# 立即启动一个服务
$ sudo systemctl start apache.service
# 立即停止一个服务
$ sudo systemctl stop apache.service
# 重启一个服务
$ sudo systemctl restart apache.service
# 杀死一个服务的所有子进程
$ sudo systemctl kill apache.service
# 重新加载一个服务的配置文件
$ sudo systemctl reload apache.service
# 重载所有修改过的配置文件
$ sudo systemctl daemon-reload
# 显示某个 Unit 的所有底层参数
$ systemctl show httpd.service
# 显示某个 Unit 的指定属性的值
$ systemctl show -p CPUShares httpd.service
# 设置某个 Unit 的指定属性
$ sudo systemctl set-property httpd.service CPUShares=500
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 四、配置示例
# (一)配置Prometheus
已知
Prometheus安装在/store_64G/opt/prometheus/prometheus-2.19.0.linux-amd64目录
- 新增
systemd配置文件:/etc/systemd/system/prometheus.service,将如下内容写入prometheus.service文件中[Unit] Description=Prometheus Wants=network-online.target After=network-online.target [Service] User=root Group=root Type=simple Restart=on-failure ExecStart=/store_64G/opt/prometheus/prometheus-2.19.0.linux-amd64/prometheus \ --config.file=/store_64G/opt/prometheus/prometheus-2.19.0.linux-amd64/prometheus.yml \ --storage.tsdb.path=/store_64G/opt/prometheus/prometheus-2.19.0.linux-amd64/data/ \ --web.console.templates=/store_64G/opt/prometheus/prometheus-2.19.0.linux-amd64/consoles \ --web.console.libraries=/store_64G/opt/prometheus/prometheus-2.19.0.linux-amd64/console_libraries \ --web.enable-admin-api \ --web.enable-lifecycle [Install] WantedBy=multi-user.target1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 - 重新加载配置:
systemctl daemon-reload - 将Prometheus设置为开机启动:
systemctl enable prometheus - 启动
Prometheus:systemctl start prometheus - 检查是否启动成功:
[root@localhost prometheus]# systemctl status prometheus ● prometheus.service - Prometheus Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2020-07-14 10:12:56 CST; 24min ago Main PID: 28424 (prometheus) CGroup: /system.slice/prometheus.service └─28424 /store_64G/opt/prometheus/prometheus-2.19.0.linux-amd64/prometheus --config.file=/store_64G/opt/prometheus/prometheus-2.19.0.linux-amd64/prometheus.yml --storage.tsdb.path... Jul 14 10:12:59 localhost.localdomain prometheus[28424]: level=info ts=2020-07-14T02:12:59.367Z caller=head.go:706 component=tsdb msg="WAL segment loaded" segment=18 maxSegment=21 Jul 14 10:12:59 localhost.localdomain prometheus[28424]: level=info ts=2020-07-14T02:12:59.447Z caller=head.go:706 component=tsdb msg="WAL segment loaded" segment=19 maxSegment=21 Jul 14 10:13:00 localhost.localdomain prometheus[28424]: level=info ts=2020-07-14T02:13:00.171Z caller=head.go:706 component=tsdb msg="WAL segment loaded" segment=20 maxSegment=21 Jul 14 10:13:00 localhost.localdomain prometheus[28424]: level=info ts=2020-07-14T02:13:00.172Z caller=head.go:706 component=tsdb msg="WAL segment loaded" segment=21 maxSegment=21 Jul 14 10:13:00 localhost.localdomain prometheus[28424]: level=info ts=2020-07-14T02:13:00.172Z caller=head.go:709 component=tsdb msg="WAL replay completed" duration=3.212636669s Jul 14 10:13:00 localhost.localdomain prometheus[28424]: level=info ts=2020-07-14T02:13:00.249Z caller=main.go:694 fs_type=EXT4_SUPER_MAGIC Jul 14 10:13:00 localhost.localdomain prometheus[28424]: level=info ts=2020-07-14T02:13:00.249Z caller=main.go:695 msg="TSDB started" Jul 14 10:13:00 localhost.localdomain prometheus[28424]: level=info ts=2020-07-14T02:13:00.249Z caller=main.go:799 msg="Loading configuration file" filename=/store_64G/opt/prometh...metheus.yml Jul 14 10:13:00 localhost.localdomain prometheus[28424]: level=info ts=2020-07-14T02:13:00.251Z caller=main.go:827 msg="Completed loading of configuration file" filename=/store_64...metheus.yml Jul 14 10:13:00 localhost.localdomain prometheus[28424]: level=info ts=2020-07-14T02:13:00.251Z caller=main.go:646 msg="Server is ready to receive web requests." Hint: Some lines were ellipsized, use -l to show in full.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# (二)配置Grafana
已知
Grafana安装在/store_64G/opt/grafana/grafana-7.0.3目录
- 新增
systemd配置文件:/etc/systemd/system/grafana.service,将如下内容写入grafana.service文件中[Unit] Description=Grafana Wants=network-online.target After=network-online.target [Service] User=root Group=root Type=simple Restart=on-failure ExecStart=/store_64G/opt/grafana/grafana-7.0.3/bin/grafana-server \ -config=/store_64G/opt/grafana/grafana-7.0.3/conf/defaults.ini \ -homepath=/store_64G/opt/grafana/grafana-7.0.3/ [Install] WantedBy=multi-user.target1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 - 重新加载配置:
systemctl daemon-reload - 将Prometheus设置为开机启动:
systemctl enable grafana - 启动
Prometheus:systemctl start grafana - 检查是否启动成功:
[root@localhost grafana]# systemctl status grafana ● grafana.service - Grafana Loaded: loaded (/etc/systemd/system/grafana.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2020-07-14 10:45:11 CST; 5s ago Main PID: 41206 (grafana-server) CGroup: /system.slice/grafana.service └─41206 /store_64G/opt/grafana/grafana-7.0.3/bin/grafana-server -config=/store_64G/opt/grafana/grafana-7.0.3/conf/defaults.ini -homepath=/store_64G/opt/grafana/grafana-7.0.3/ Jul 14 10:45:11 localhost.localdomain grafana-server[41206]: t=2020-07-14T10:45:11+0800 lvl=info msg="Path Data" logger=settings path=/store_64G/opt/grafana/grafana-7.0.3/data Jul 14 10:45:11 localhost.localdomain grafana-server[41206]: t=2020-07-14T10:45:11+0800 lvl=info msg="Path Logs" logger=settings path=/store_64G/opt/grafana/grafana-7.0.3/data/log Jul 14 10:45:11 localhost.localdomain grafana-server[41206]: t=2020-07-14T10:45:11+0800 lvl=info msg="Path Plugins" logger=settings path=/store_64G/opt/grafana/grafana-7.0.3/data/plugins Jul 14 10:45:11 localhost.localdomain grafana-server[41206]: t=2020-07-14T10:45:11+0800 lvl=info msg="Path Provisioning" logger=settings path=/store_64G/opt/grafana/grafana-7.0.3/...rovisioning Jul 14 10:45:11 localhost.localdomain grafana-server[41206]: t=2020-07-14T10:45:11+0800 lvl=info msg="App mode production" logger=settings Jul 14 10:45:11 localhost.localdomain grafana-server[41206]: t=2020-07-14T10:45:11+0800 lvl=info msg="Connecting to DB" logger=sqlstore dbtype=sqlite3 Jul 14 10:45:11 localhost.localdomain grafana-server[41206]: t=2020-07-14T10:45:11+0800 lvl=info msg="Starting DB migration" logger=migrator Jul 14 10:45:11 localhost.localdomain grafana-server[41206]: t=2020-07-14T10:45:11+0800 lvl=info msg="Starting plugin search" logger=plugins Jul 14 10:45:11 localhost.localdomain grafana-server[41206]: t=2020-07-14T10:45:11+0800 lvl=info msg="Registering plugin" logger=plugins name="Direct Input" Jul 14 10:45:11 localhost.localdomain grafana-server[41206]: t=2020-07-14T10:45:11+0800 lvl=info msg="HTTP Server Listen" logger=http.server address=[::]:3000 protocol=http subUrl= socket= Hint: Some lines were ellipsized, use -l to show in full.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# (三)配置Node-Exporter
已知
node-exporter安装在/store_64G/opt/node_exporter/node_exporter-1.0.0.linux-amd64目录
新增
systemd配置文件:/etc/systemd/system/node_exporter.service,将如下内容写入node_exporter.service文件中[Unit] Description=Node Exporter Wants=network-online.target After=network-online.target [Service] User=root Group=root Type=simple Restart=on-failure ExecStart=/store_64G/opt/node_exporter/node_exporter-1.0.0.linux-amd64/node_exporter [Install] WantedBy=multi-user.target1
2
3
4
5
6
7
8
9
10
11
12
13
14重新加载配置:
systemctl daemon-reload将
Node-Exporter设置为开机启动:systemctl enable node_exporter启动
Node-Exporter:systemctl start node_exporter检查是否启动成功:
[root@localhost grafana]# systemctl status node_exporter ● node_exporter.service - Node Exporter Loaded: loaded (/etc/systemd/system/node_exporter.service; enabled; vendor preset: disabled) Active: active (running) since Tue 2020-07-14 11:22:26 CST; 7s ago Main PID: 55513 (node_exporter) CGroup: /system.slice/node_exporter.service └─55513 /store_64G/opt/node_exporter/node_exporter-1.0.0.linux-amd64/node_exporter Jul 14 11:22:26 localhost.localdomain node_exporter[55513]: level=info ts=2020-07-14T03:22:26.720Z caller=node_exporter.go:112 collector=thermal_zone Jul 14 11:22:26 localhost.localdomain node_exporter[55513]: level=info ts=2020-07-14T03:22:26.720Z caller=node_exporter.go:112 collector=time Jul 14 11:22:26 localhost.localdomain node_exporter[55513]: level=info ts=2020-07-14T03:22:26.720Z caller=node_exporter.go:112 collector=timex Jul 14 11:22:26 localhost.localdomain node_exporter[55513]: level=info ts=2020-07-14T03:22:26.720Z caller=node_exporter.go:112 collector=udp_queues Jul 14 11:22:26 localhost.localdomain node_exporter[55513]: level=info ts=2020-07-14T03:22:26.720Z caller=node_exporter.go:112 collector=uname Jul 14 11:22:26 localhost.localdomain node_exporter[55513]: level=info ts=2020-07-14T03:22:26.720Z caller=node_exporter.go:112 collector=vmstat Jul 14 11:22:26 localhost.localdomain node_exporter[55513]: level=info ts=2020-07-14T03:22:26.721Z caller=node_exporter.go:112 collector=xfs Jul 14 11:22:26 localhost.localdomain node_exporter[55513]: level=info ts=2020-07-14T03:22:26.721Z caller=node_exporter.go:112 collector=zfs Jul 14 11:22:26 localhost.localdomain node_exporter[55513]: level=info ts=2020-07-14T03:22:26.721Z caller=node_exporter.go:191 msg="Listening on" address=:9100 Jul 14 11:22:26 localhost.localdomain node_exporter[55513]: level=info ts=2020-07-14T03:22:26.721Z caller=tls_config.go:170 msg="TLS is disabled and it cannot be enabled on the fl...http2=false Hint: Some lines were ellipsized, use -l to show in full.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
参考资料:
《Systemd 入门教程:命令篇 - 阮一峰的网络日志》