golang 管理 pidfile
Pidfile 存储了进程的进程 id。一般情况下 pidfile 有以下几个作用:
- 其他进程可以读取 pidfile 获取运行进程的 pid(当然也可以通过其他命令 动态获取)
- 在启动进程前先检查 pidfile,防止启动多个后台进程(特别像 agent 之类的)
下面是 docker 中管理 pidfile 的方法:
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
)
func main() {
_, err := NewPIDFile("pid.file")
if err != nil {
log.Errorf("error to create the pid file failed:%s", err.Error())
}
}
// PIDFile stored the process id
type PIDFile struct {
path string
}
// just suit for linux
func processExists(pid string) bool {
if _, err := os.Stat(filepath.Join("/proc", pid)); err == nil {
return true
}
return false
}
func checkPIDFILEAlreadyExists(path string) error {
if pidByte, err := ioutil.ReadFile(path); err == nil {
pid := strings.TrimSpace(string(pidByte))
if processExists(pid) {
return fmt.Errorf("ensure the process:%s is not running pid file:%s", pid, path)
}
}
return nil
}
// NewPIDFile create the pid file
// 指定路径下生产 pidfile, 文件内容为 pid
func NewPIDFile(path string) (*PIDFile, error) {
if err := checkPIDFILEAlreadyExists(path); err != nil {
return nil, err
}
if err := os.MkdirAll(filepath.Dir(path), os.FileMode(0755)); err != nil {
return nil, err
}
if err := ioutil.WriteFile(path, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
return nil, err
}
return &PIDFile{path: path}, nil
}
// Remove remove the pid file
func (file PIDFile) Remove() error {
return os.Remove(file.path)
}
Tips:
- 不同操作系统判断进程是否存在的方式是不一样的.文中的是检测 linux 下进程是否存在的方法
golang 管理 pidfile的更多相关文章
- go-zero单体服务使用泛型简化注册Handler路由
一.Golang环境安装及配置Go Module https://go-zero.dev/cn/docs/prepare/golang-install mac OS安装Go# 下载并安装Go for ...
- Golang学习--包管理工具glide
上一篇文章中我们已经成功的运行了go的代码,这是我们迈出的最基础的一步. 一个项目通常会依赖很多外部的库,当依赖的库比较多的时候,手工管理就会比较麻烦,这个时候就需要包管理工具出场了,帮你管理好所有依 ...
- hbase rest api接口链接管理【golang语言版】
# go-hbase-resthbase rest api接口链接管理[golang语言版]关于hbase的rest接口的详细信息可以到官网查看[http://hbase.apache.org/boo ...
- Golang 的内存管理(上篇)
Golang 的内存管理基于 tcmalloc,可以说起点挺高的.但是 Golang 在实现的时候还做了很多优化,我们下面通过源码来看一下 Golang 的内存管理实现.下面的源码分析基于 go1.8 ...
- golang项目git-subtree完美解决差异包管理
目标: 1.把golang官方已移动过url的包跟随自己的项目git代码上传到项目源码中. 2.把或自己修改过的差异化fork包跟随自己的项目git代码上传到项目源码中. 解决方案: 方案1.第三方包 ...
- Golang的模块管理Module
Golang 1.11版本终于支持了官方的模块依赖管理功能,1.11以前想要实现依赖管理只能够通过借助第三方库来实现,1.11以前的版本Golang项目必须依赖以GOPATH,从当前版本开始Golan ...
- golang的包管理---vendor/dep等
首先关于vendor 1 提出问题 我们知道,一个工程稍大一点,通常会依赖各种各样的包.而Go使用统一的GOPATH管理依赖包,且每个包仅保留一个版本.而不同的依赖包由各自的版本工具独立管理,所以当所 ...
- Golang 包管理简介
Golang 包管理 在一个项目里,如果想引用本地包,经常会把新手搞的莫名其妙.这里通俗记录一下. 首先先要知道几个默认的规则 必须定义环境变量GOPATH,GOPATH可以定义多个目录 所有项目代码 ...
- golang包管理解决之道——go modules初探
golang的包管理是一直是为人诟病之处,从golang1.5引入的vendor机制,到准官方工具dep,目前为止还没一个简便的解决方案. 不过现在go modules随着golang1.11的发布而 ...
随机推荐
- python 设计模式之原型模式 Prototype Pattern
#引入 例子1: 孙悟空拔下一嘬猴毛,轻轻一吹就会变出好多的孙悟空来. 例子2:寄个快递下面是一个邮寄快递的场景:“给我寄个快递.”顾客说.“寄往什么地方?寄给……?”你问.“和上次差不多一样,只是邮 ...
- Android开发Camera2相关
Android自定义相机 https://github.com/miqt/camera2 Camera2必知必会 https://www.cnblogs.com/fuyaozhishang/p/975 ...
- ubuntu18源码包安装openresty
author: headsen chen date : 2019-07-30 15:42:24 #在ubuntu18.04 环境下,openresty的依赖库有:PCRE.OpenSSL.zlib, ...
- Web 性能压力测试工具之 Siege 详解
Siege是一款开源的压力测试工具,设计用于评估WEB应用在压力下的承受能力.可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数量的并发访问下重复进行.s ...
- flutter中使用shared_preferences的存储
添加依赖 shared_preferences: ^+ 工具类 import 'dart:async'; import 'package:shared_preferences/shared_prefe ...
- openresty开发系列12--lua介绍及常用数据类型简介
openresty开发系列12--lua介绍及常用数据类型简介 lua介绍 1993 年在巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de ...
- 006-guava 集合-集合工具类-集合扩展工具类
一.概述 需要实现自己的集合扩展.也许你想要在元素被添加到列表时增加特定的行为,或者你想实现一个Iterable,其底层实际上是遍历数据库查询的结果集 二.使用 2.1.ForwardingList装 ...
- online学习和offline学习
参考:https://blog.csdn.net/a133521741/article/details/79221015 解释: (1)offline学习:每次训练完一个batch后再更新参数: (2 ...
- Python - Django - session 的基本使用
urls.py: from django.conf.urls import url from app02 import views urlpatterns = [ # app02 url(r'^app ...
- 【Leetcode_easy】922. Sort Array By Parity II
problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...