A-speed

每个人都是🏆
  新随笔  :: 管理

Grafana systemd 服务托管与开机自启

Posted on 2026-10-10 13:55  a-speed  阅读(5)  评论(0)    收藏  举报

Grafana systemd 服务托管与开机自启

环境:node2 (192.168.56.132)・Grafana 6.0.1・RHEL 8.6
整理日期:2026-10-10
目标:把手动 nohup 启动的 Grafana 改为 systemd 服务托管,后台自动启动


1. 背景与问题

原始状态:Grafana 是用 nohup bin/grafana-server 手动启动的(进程 root 1599),存在以下问题:

  • 不是 systemd 托管 → 无开机自启、无法自动重启。
  • 进程崩溃后不会自动拉起。
  • 之前日志出现不稳定(proxy error: context canceled 等)与手动启动方式有关。

旧服务残留:/etc/systemd/system/grafana-3000.service 指向不存在的 /tidb-deploy/grafana-3000/scripts/run_grafana.sh(TiDB 部署残留),无效且 disabled,无法复用。


2. 新增 systemd 服务

2.1 服务文件 /etc/systemd/system/grafana.service

[Unit]
Description=Grafana Server (6.0.1)
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/root/grafana-6.0.1
ExecStart=/root/grafana-6.0.1/bin/grafana-server \
  --homepath=/root/grafana-6.0.1 \
  --config=/root/grafana-6.0.1/conf/defaults.ini
Restart=on-failure
RestartSec=5s
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

关键参数:

  • WorkingDirectory=/root/grafana-6.0.1:Grafana 依赖运行目录(数据、插件、public 资源)。
  • --homepath:指定 home 目录。
  • --config=/root/grafana-6.0.1/conf/defaults.ini:默认配置(http_port=3000)。
  • Restart=on-failure:异常退出自动重启。
  • 数据目录沿用 /root/grafana-6.0.1/data/grafana.db(不清空,面板/数据源不丢失)。

2.2 启动与自启

# 停掉手动 nohup 进程
kill 1599

# 创建服务(写上面的 grafana.service)

# 重载并启动
systemctl daemon-reload
systemctl start grafana
systemctl enable grafana   # 开机自启

3. 验证结果

检查项 结果
服务状态 active (running),Main PID 1991
开机自启 enabled ✅
端口 3000 LISTEN ✅(pid 1991)
HTTP 访问 curl http://127.0.0.1:3000/login → HTTP 200 ✅
进程方式 systemd /root/grafana-6.0.1/bin/grafana-server(不再 nohup)

4. 维护命令

# 状态
systemctl status grafana --no-pager
systemctl is-active grafana
systemctl is-enabled grafana

# 启停重启
systemctl start grafana
systemctl stop grafana
systemctl restart grafana

# 查看日志
journalctl -u grafana --no-pager -n 100

# 重新加载配置(改 defaults.ini 后)
systemctl restart grafana

5. 注意事项

  • 旧服务 grafana-3000.service(TiDB 残留):已 disabled,无影响,保留未删。
  • 数据保留:改由 systemd 启动后,仍使用 /root/grafana-6.0.1/data/,面板、数据源、admin 密码(Lihj2026)均不变。
  • 开机自启:systemctl enable grafana 已生效,重启系统后 Grafana 自动运行。

6. 与其他服务的关系

Grafana 依赖 Prometheus(9090) 数据源。建议启动顺序(systemd 的 After=network.target 已保证网络就绪,Prometheus 单独 enable 自启):

  1. Prometheus(9090) → 已 enable 自启
  2. Grafana(3000) → 本次已 enable 自启
  3. 各 exporter(MySQL 9104/9105/9106、PG 9187/9188/9189)→ 已 enable 自启

本文档记录 2026-10-10 Grafana 改为 systemd 托管并设置开机自启,命令均经实际执行验证。