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的发布而 ...
随机推荐
- 腾讯云CENTOS7安装MSSQL2017
腾讯云CENTOS7安装MSSQL2017 mkdir -p /opt/sqlserver2017cd /opt/sqlserver2017/ 下载离线包:wget https://packages. ...
- spring线程池的同步和异步(1)
spring线程池(同步.异步) 一.spring异步线程池类图 二.简单介绍 2.1. TaskExecutor---Spring异步线程池的接口类,其实质是java.util.concurrent ...
- c++ 函数后面加一个冒号的含义
转载自:https://zhidao.baidu.com/question/2010930169328038188.html 冒号后面跟的是赋值,这种写法是C++的特性. A( int aa, int ...
- GB28181技术基础之3 - RTP
一. RTP协议 实时传输协议 RTP(Real-time Transport Protocol)是一个网络传输协议,它是由IETF的多媒体传输工作小组1996年在RFC 1889中公布的,后在RFC ...
- Springboot配置文件获取系统环境变量的值
注意,这里说的是获取系统环境变量的值,譬如Windows里配置的JAVA_HOME之类的,可以直接在Springboot的配置文件中获取. 我们经常使用一些docker管理平台,如DaoCloud.r ...
- PHP如何防止注入及开发安全
1.PHP注入的基本原理 程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候,没有对 用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据 库查询代码,根据程序返回 ...
- typeScript模块<二>
/*模块 1 模块的的概念 2 模块导出的几种方法 1.export 导出声明 2.export 导出语句 3.export default 4.import导入模块 3 模块化封装上一讲的DB库 * ...
- centos7安装python3.6独立的virtualenv环境
centos7安装python3.6独立的virtualenv环境 1.编译安装python3.6环境# 安装依赖yum -y install zlib-devel bzip2-devel opens ...
- 1. Tomcat之startup.sh
#判断操作系统os400=falsecase "`uname`" inOS400*) os400=true;;esac # 取脚本名称PRG="$0" # 判断 ...
- sqllite connectionstring setting
https://www.connectionstrings.com/sqlite/ SQLite.NET Basic Data Source=c:\mydb.db;Version=3; Version ...