0x00 前言
场景如下:
- 服务开机启动
- 需要时刻监测某个重要服务是否在线,如果程序报错退出则自动重启服务,以此来保持服务常在线
systemd简介
1、基本操作,假设服务程序路径部署在/usr/local/bin/systemd-test
,配置存储在/etc/systemd/system/systemd-test.service
[Unit]
Description=Systemd Test
After=network.target
[Service]
User=nobody
# Execute `systemctl daemon-reload` after ExecStart= is changed.
ExecStart=/usr/local/bin/systemd-test
[Install]
WantedBy=multi-user.target
# 每一次修改ExecStart都需执行
systemctl daemon-reload
# 启动
systemctl start systemd-test.service
# 查看状态
systemctl status systemd-test.service
0x01 Nginx配置
0x02 两个例子
1、case1:
func PANIC() {
time.Sleep(30 * time.Second)
panic("PANIC")
}
func main() {
l, err := net.Listen("tcp", ":8081")
if err != nil {
log.Panicf("cannot listen: %s", err)
}
go PANIC()
http.Serve(l, nil)
}
[root@VM-X-X-centos notify]# cat /etc/systemd/system/systemd-test.service
[Unit]
Description=Systemd Test
After=network.target
[Service]
Type=simple
User=nobody
# Execute `systemctl daemon-reload` after ExecStart= is changed.
ExecStart=/usr/local/bin/systemd-test
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
2、case2
func a() {
for {
_, err := http.Get("http://127.0.0.1:8081") // ❸
if err == nil {
daemon.SdNotify(false, daemon.SdNotifyWatchdog)
}
time.Sleep(3 * time.Second)
}
}
func b() {
time.Sleep(30 * time.Second)
panic("panic")
}
func main() {
l, err := net.Listen("tcp", ":8081")
if err != nil {
log.Panicf("cannot listen: %s", err)
}
daemon.SdNotify(false, daemon.SdNotifyReady)
go a()
go b()
http.Serve(l, nil)
}
[root@VM-X-X-centos notify]# cat /etc/systemd/system/systemd-test.service
[Unit]
Description=Systemd Test
After=network.target
[Service]
Type=notify
User=nobody
# Execute `systemctl daemon-reload` after ExecStart= is changed.
ExecStart=/usr/local/bin/systemd-test
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
0x0 参考
- Go bindings to systemd socket activation, journal, D-Bus, and unit files
- Golang应用结合systemd注册服务为守护进程
- 利用 systemd 部署 golang 项目
- Integration of a Go service with systemd: readiness & liveness
FEATURED TAGS
Latex
gRPC
负载均衡
OpenSSH
Authentication
Consul
Etcd
Kubernetes
性能优化
Python
分布式锁
WebConsole
后台开发
Golang
OpenSource
Nginx
Vault
网络安全
Perl
分布式理论
Raft
正则表达式
Redis
分布式
限流
go-redis
微服务
反向代理
ReverseProxy
Cache
缓存
连接池
OpenTracing
GOMAXPROCS
GoMicro
微服务框架
日志
zap
Pool
Kratos
Hystrix
熔断
并发
Pipeline
证书
Prometheus
Metrics
PromQL
Breaker
定时器
Timer
Timeout
Kafka
Xorm
MySQL
Fasthttp
bytebufferpool
任务队列
队列
异步队列
GOIM
Pprof
errgroup
consistent-hash
Zinx
网络框架
设计模式
HTTP
Gateway
Queue
Docker
网关
Statefulset
NFS
Machinery
Teleport
Zero Trust
Oxy
存储
Confd
热更新
OAuth
SAML
OpenID
Openssl
AES
微服务网关
IM
KMS
安全
数据结构
hashtable
Sort
Asynq
基数树
Radix
Crontab
热重启
系统编程
sarama
Go-Zero
RDP
VNC
协程池
UDP
hashmap
网络编程
自适应技术
环形队列
Ring Buffer
Circular Buffer
InnoDB
timewheel
GroupCache
Jaeger
GOSSIP
CAP
Bash
websocket
事务
GC
TLS
singleflight
闭包
Helm
network
iptables
MITM
HTTPS
Tap
Tun
路由
wireguard
gvisor
Git
NAT
协议栈
Envoy
FRP
DPI
gopacket
Cgroup
Namespace
DNS
eBPF
GoZero
Gost
Clash
gopsutil
HIDS
ELKEID
XDP
TC
Linux
Systemd
netlink