golang实现注册系统服务(Windows、Darwin)

golang实现注册系统服务(Windows、Darwin)

仓库地址:https://github.com/ziyifast/yiSystemService

使用第三方包:go get “github.com/kardianos/service”
日志库:go get “github.com/sirupsen/logrus”

  • log “github.com/sirupsen/logrus”

1 初始化日志

util/log.go:

package utilimport (log "github.com/sirupsen/logrus""io""os"
)func InitLog(logPath string) {//设置输出样式,自带的只有两种样式logrus.JSONFormatter{}和logrus.TextFormatter{}log.SetFormatter(&log.TextFormatter{})log.SetOutput(os.Stdout)//设置output,默认为stderr,可以为任何io.Writer,比如文件*os.Filefile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)writers := []io.Writer{file,os.Stdout}//同时写文件和屏幕fileAndStdoutWriter := io.MultiWriter(writers...)if err == nil {log.SetOutput(fileAndStdoutWriter)} else {log.Info("failed to log to file.")}//设置最低loglevellog.SetLevel(log.InfoLevel)
}

2 导入三方Service库+编写serviceConfig(描述、可执行文件路径等)

2.1 consts

①consts/consts.go:

package constsimport "path"const (ServiceVersion     = "v1.0"ServiceName        = "yiService"ServicePort        = 9999ServiceDisplayName = "yi.service"ServiceDescription = "my test service"
)var LogPath stringfunc init() {LogPath = path.Join(WorkDir, "yiService.log")
}

②consts/consts_darwin.go:

//go:build darwin
// +build darwinpackage constsimport "path/filepath"var (StartupBash  = filepath.Join(WorkDir, "startup.sh")ShutdownBash = filepath.Join(WorkDir, "shutdown.sh")
)const (WorkDir = "/usr/local/yiService"MainExe = "yiService"
)    

③consts/consts_windows.go:

package constsimport "path/filepath"var (StartupBash  = filepath.Join(WorkDir, "startup.bat")ShutdownBash = filepath.Join(WorkDir, "shutdown.bat")
)const (WorkDir    = "c:/yiService"MainExe    = "yiService.exe"
)

2.2 system_service

system_service/system_service.go:

package system_serviceimport ("awesomeProject1/consts""fmt""github.com/kardianos/service"log "github.com/sirupsen/logrus""os""runtime"
)var (logger service.Loggersvc    service.Service
)func init() {svcConfig := &service.Config{Name:             consts.ServiceName,WorkingDirectory: consts.WorkDir,DisplayName:      consts.ServiceDisplayName,Description:      consts.ServiceDescription,Arguments:        []string{"service", "run"}, //服务注册成功之后,由服务去执行yiService.exe service run【运行服务】Executable:       fmt.Sprintf("%s/%s", consts.WorkDir, consts.ServiceName),Option:           service.KeyValue{},}if runtime.GOOS == "windows" {svcConfig.Executable = fmt.Sprintf("%s\\%s.exe", consts.WorkDir, consts.ServiceName)}var err errorprogram := &Program{}svc, err = service.New(program, svcConfig)if err != nil {log.Errorf("create service fail %v\n", err)return}errChan := make(chan error, 5)logger, err = svc.Logger(errChan)if err != nil {log.Errorf("%v\n", err)return}if err != nil {log.Errorf("%v\n", err)return}go func() {log.Info("watching err chan....")for {err := <-errChanif err != nil {log.Fatalf("service err %v", err)}}}()
}func StartSVC() {log.Infof("StartSVC...")serviceControl("install")serviceControl("start")
}func StopSVC() {log.Infof("try to stop service, if already exists.")serviceControl("stop")
}func RunSVC() {fmt.Sprintf("%s service running \n", runtime.GOOS)if err := svc.Run(); err != nil {fmt.Sprintf("%s service running fail %v \n", runtime.GOOS, err)os.Exit(1)}
}func serviceControl(action string) {log.Infof("%s service %s \n", runtime.GOOS, action)if err := service.Control(svc, action); err != nil {log.Infof("%s service: %v \n", action, err)}
}

3 编写服务脚本(startup、shutdown脚本)

3.1 startup脚本

  1. assets/startup.bat.tmpl
@echo off@REM It is recommended to start here.
@REM Modify bash args below.
@REM "name" and "tags" must be alphanumeric sequence: [a-zA-Z-_.]
@REM Chinese chars are not supported here in bash file.
@REM
@REM Example:
@REM
@REM yiService.exe service start ^
@REM
@REM Startup from here
@REM
c:/yiService/yiService.exe service startecho "yiService started!"
echo ""
pause
  1. assets/startup.sh.tmpl
#!/bin/bash# It is recommended to start here.
# Modify bash args below.
# "name" and "tags" must be alphanumeric sequence: [a-zA-Z-_.]
# Chinese chars are not supported here in bash file.
#
# Example:
#
# yiService.exe service start \
#
# Startup from here
#
# launchctl start yiService
./yiService service startecho yiService started!
ps aux |grep yiService
echo ""

3.2 shutdown脚本

  1. assets/shutdown.bat.tmpl
@echo off
@REM This command bash will stop the yiService windows service
@REM And then uninstall this service from operation system
@REM Configurations will be remained in directory c:/yiService/yiService on the disk.
@REM You can restart from those configurations in the near future.
@REM
c:/yiService/yiService.exe service stop
set "$process=yiService.exe"
for %%a in (%$process%) do tasklist /fi "imagename eq %%a"  | find "%%a" && taskkill /f /im %%aecho shutdown yiService successfully!
pause
  1. assets/shutdown.sh.tmpl
#!/bin/bash# This command bash will stop the yiService windows service
# And then uninstall this service from operation system
# Configurations will be remained in directory c:/yiService on the disk.
# You can restart from those configurations in the near future.
#./yiService service stop
PID=$(ps -eaf | grep '/usr/local/yiService' | grep -v grep | awk '{print $2}')
if [[ "" !=  "$PID" ]]; thenecho "killing $PID"kill -9 "$PID"
fiecho shutdown yiService successfully!

4 编写extractFiles(执行exe文件之后,拷贝exe到服务工作目录)

system_service/extract_files.go:

package system_serviceimport ("awesomeProject1/assets""awesomeProject1/consts""fmt"log "github.com/sirupsen/logrus""io""os""runtime"
)func ExtractFiles() {CopyMainExe()//copy startup\shutdownif err := os.WriteFile(consts.StartupBash, assets.StartupBatTmpl, os.ModePerm); err != nil {log.Errorf("create startup bash failed %v", err)}if err := os.WriteFile(consts.ShutdownBash, assets.ShutdownBatTmpl, os.ModePerm); err != nil {log.Errorf("create shutdown bash failed %v", err)}
}func CopyMainExe() {executable, err := os.Executable()log.Infof("install %s to %s", executable, consts.MainExe)if err != nil {log.Errorf("%v", err)}sFile, err := os.Open(executable)if err != nil {log.Errorf("%v", err)}defer sFile.Close()exePath := fmt.Sprintf("%s/%s", consts.WorkDir, consts.MainExe)if runtime.GOOS == "windows" {exePath = fmt.Sprintf("%s\\%s", consts.WorkDir, consts.MainExe)}_, err = os.Stat(exePath)if err == nil {//overwriteif err := os.RemoveAll(exePath); err != nil {log.Errorf("%v", err)}}eFile, err := os.Create(exePath)if err != nil {log.Errorf("%v", err)}defer eFile.Close()if _, err = io.Copy(eFile, sFile); err != nil {log.Errorf("%v", err)}if err = eFile.Sync(); err != nil {log.Errorf("%v", err)}if err = os.Chdir(consts.WorkDir); err != nil {log.Errorf("%v\n", err)}if err = os.Chmod(consts.MainExe, os.FileMode(0777)); err != nil {log.Errorf("%v", err)}
}

5 编写firewall部分+main函数部分(开放端口+由系统服务拉起进程)

firewall部分:以管理员身份运行,开放进程服务端口
cmd/main.go部分:初次运行尝试卸载服务,带os.Args参数运行启动服务

5.1 firewall部分

  1. darwin

system_service/firewall/firewall_darwin.go:

//go:build darwin
// +build darwinpackage firewallimport ("awesomeProject1/consts""bytes""fmt"log "github.com/sirupsen/logrus""os/exec""strconv""strings"
)func OpenPort() {log.Infof("darwin firewall checking\n")cmd0 := exec.Command("/usr/bin/nc", "-z", "127.0.0.1", fmt.Sprintf("%d", consts.ServicePort))log.Warnf("cmd0=%s\n", cmd0)stdout, err := cmd0.CombinedOutput()result := string(stdout)if err != nil {log.Infof("err=%v \n", err)return}if strings.Contains(result, "command not found") {fmt.Println("[warn]:", result)return}if strings.Contains(result, "not running") {fmt.Println("[warn]:", result)return}if strings.Contains(result, strconv.Itoa(consts.ServicePort)) {log.Warnf("%d already opened\n", consts.ServicePort)return}cmd := exec.Command("bash", "-c", fmt.Sprintf("firewall-cmd --zone=public --add-port=%d/tcp --permanent && firewall-cmd --reload", consts.ServicePort))var out bytes.Buffervar stderr bytes.Buffercmd.Stdout = &outcmd.Stderr = &stderrif err := cmd.Run(); err != nil {log.Warnf("%s", stderr.String())log.Warnf("%v", err)}log.Warnf(out.String())
}
  1. Windows
    system_service/firewall/firewall_windows.go
//go:build windows
// +build windowspackage firewallimport ("bytes""fmt"log "github.com/sirupsen/logrus""os/exec""runtime"
)func OpenPort() {log.Infot("windows firewall checking")cmd := exec.Command("cmd", "/c", "netsh advfirewall firewall delete rule name=\"yiService\"")var out bytes.Buffervar stderr bytes.Buffercmd.Stdout = &outcmd.Stderr = &stderrif runtime.GOOS == "windows" {}if err := cmd.Run(); err != nil {log.Errorf("%s", stderr.String())log.Errorf("%v", err)}cmd2 := exec.Command("cmd", "/c",fmt.Sprintf("netsh advfirewall firewall add rule name=\"yiService\" dir=in action=allow protocol=TCP localport=%d",consts.ServicePort,))var out2 bytes.Buffervar stderr2 bytes.Buffercmd2.Stdout = &out2cmd2.Stderr = &stderr2if runtime.GOOS == "windows" {}if err := cmd2.Run(); err != nil {log.Errorf("%s", stderr2.String())log.Errorf("%v", err)}
}

5.2 main函数

cmd/main.go

package mainimport ("awesomeProject1/consts""awesomeProject1/system_service""awesomeProject1/system_service/firewall""awesomeProject1/util"log "github.com/sirupsen/logrus""os"
)func init() {//os.Setenv("dev", "true")util.InitLog(consts.LogPath)
}func main() {if len(os.Getenv("dev")) != 0 {system_service.StopSVC()firewall.OpenPort()system_service.StartSVC()system_service.RunSVC()}log.Errorf("os.Args=%v len=%v \n", os.Args, len(os.Args))if len(os.Args) == 1 {//stop svc if existsystem_service.StopSVC()log.Errorf("install %v \n", consts.ServiceName)if err := os.MkdirAll(consts.WorkDir, os.ModePerm); err != nil {log.Errorf("%v\n", err)}firewall.OpenPort()system_service.ExtractFiles()pwd, err := os.Getwd()if err != nil {log.Errorf("%v\n", err)}log.Infof("install svc, working directory %s", pwd)system_service.StartSVC()log.Infof("yiService installed!")return}os.Chdir(consts.WorkDir)log.Errorf("service %s \n", os.Args[2])switch os.Args[2] {case "start":system_service.StartSVC()returncase "stop":system_service.StopSVC()returndefault:system_service.RunSVC()log.Info("running yiService")}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/233947.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Qt Q_DECL_OVERRIDE

Q_DECL_OVERRIDE也就是C的override&#xff08;重写函数&#xff09;&#xff0c;其目的就是为了防止写错虚函数,在重写虚函数时需要用到。 /* 鼠标按下事件 */ void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; 参考: Qt Q_DECL_OVERRIDE - 一杯清酒邀明月 - 博客…

Mybatis复习总结

MyBatis是一款优秀的持久层框架&#xff0c;用于简化JDBC的开发 MyBatis本是Apache的一个开源项目&#xff0c;2010年这个项目由apache迁移到了Google&#xff0c;并且改名为 Mybatis&#xff0c;2013年11月迁移至Github。 持久层 指的就是数据访问层&#xff0c;用来操作数…

图用邻接表表示的深度优先和广度优先遍历

邻接表表示法进行深度优先遍历的示例代码&#xff1a; #include <stdio.h> #include <stdlib.h>#define MAX_VERTEX_NUM 100// 边表节点结构体 typedef struct ArcNode {int adjvex; // 邻接顶点下标struct ArcNode* nextarc; // 指向下一个邻…

数组指定部分逆序重放

系列文章目录 进阶的卡莎C++_睡觉觉觉得的博客-CSDN博客数1的个数_睡觉觉觉得的博客-CSDN博客双精度浮点数的输入输出_睡觉觉觉得的博客-CSDN博客足球联赛积分_睡觉觉觉得的博客-CSDN博客大减价(一级)_睡觉觉觉得的博客-CSDN博客小写字母的判断_睡觉觉觉得的博客-CSDN博客纸币(…

pgsql的jsonb相关处理及样例

目录 1、某个字段中包含目标list中的全部使用>&#xff1a; 2、某个字段中包含目标list中任意值使用?|&#xff1a; 3、其他操作样例&#xff1a; 1、某个字段中包含目标list中的全部使用>&#xff1a; SELECT * FROM "public"."t_a" WHERE a::j…

ubuntu 20.04 docker prometheus

ubuntu 20.04 docker https://docs.docker.com/engine/install/ubuntu/ Ubuntu20.04下部署linux资源监控平台&#xff08;docker部署&#xff09;grafanaprometheusnode_exporter&#xff08;docker离线包&#xff09; https://blog.csdn.net/deer_cui/article/details/1340208…

基于ssm物资进销存论文

摘 要 现代经济快节奏发展以及不断完善升级的信息化技术&#xff0c;让传统数据信息的管理升级为软件存储&#xff0c;归纳&#xff0c;集中处理数据信息的管理方式。本货物进销管理系统就是在这样的大环境下诞生&#xff0c;其可以帮助管理者在短时间内处理完毕庞大的数据信息…

day15-动画和路由过渡和切换

&#x1f4da; 目录 介绍介绍 AnimationCurveAnimationControllerTween监听动画 自定义路由切换动画Hero飞行动画交织动画动画切换组件 AnimatedSwitcherAnimatedSwitcher封装 动画过渡组件 本文学习和引用自《Flutter实战第二版》&#xff1a;作者&#xff1a;杜文 1. 介绍 …

C#中HttpWebRequest的用法

前言 HttpWebRequest是一个常用的类&#xff0c;用于发送和接收HTTP请求。在C#中使用HttpWebRequest可以实现各种功能&#xff0c;包括发送GET和POST请求、处理Cookie、设置请求头、添加参数等。本文将深入介绍HttpWebRequest的用法&#xff0c;并给出一些常见的示例。 目录 前…

Java面试总结——集合篇

摘自javaguide的集合总体框架图&#xff1a; List, Set, Queue, Map 的区别 List&#xff1a;底层基于object[]数组&#xff0c;存储的元素有序、可重复。 Set&#xff1a;底层基于HashMap实现&#xff0c;存储的元素无序&#xff0c;不可重复。 Queue&#xff1a;单…

科研院校和研究所都在用功率放大器做哪些实验

科研院校和研究所在科研工作中常常使用功率放大器进行实验。功率放大器是一种电子设备&#xff0c;其主要功能是将输入信号的功率增加到预定的输出功率水平&#xff0c;并保持信号的波形不失真。它在各个学科领域都有广泛的应用&#xff0c;包括通信、无线电、雷达、生物医学等…

vue3 使用<script lang=“ts“ setup>加上lang=“ts“后编译错误

报错信息 报错原因 加上了langts解决 下载typescript和loader npm install typescript ts-loader --save-dev配置vue.config.js 添加下面的代码 configureWebpack: { resolve: { extensions: [".ts", ".tsx", ".js", ".json"] }…

Axure中继器的使用

一.中继器介绍 在Axure中&#xff0c;中继器&#xff08;Relays&#xff09;是一种功能强大的元件&#xff0c;可以用于创建可重复使用的模板或组件。中继器允许您定义一个主要的模板&#xff0c;并在页面中重复使用该模板的实例。以下是中继器的作用和优缺点&#xff1a; 作…

Wireshark在移动网络中的应用

第一章&#xff1a;Wireshark基础及捕获技巧 1.1 Wireshark基础知识回顾 1.2 高级捕获技巧&#xff1a;过滤器和捕获选项 1.3 Wireshark与其他抓包工具的比较 第二章&#xff1a;网络协议分析 2.1 网络协议分析&#xff1a;TCP、UDP、ICMP等 2.2 高级协议分析&#xff1a;HTTP…

2023 英特尔On技术创新大会直播 |我感受到的AI魅力

文章目录 前言英特尔技术创新大会 的来历芯生无限 赋能AI创新后记 前言 近年来&#xff0c;人工智能&#xff08;Artificial Intelligence&#xff09;的应用与发展呈现出爆发式增长的态势&#xff0c;成为科技领域最为引人注目的热门话题之一。作为全球领先的半导体公司&…

workflow系列教程(5-1)HTTP Server

往期教程 如果觉得写的可以,请给一个点赞关注支持一下 观看之前请先看,往期的博客教程,否则这篇博客没办法看懂 workFlow c异步网络库编译教程与简介 C异步网络库workflow入门教程(1)HTTP任务 C异步网络库workflow系列教程(2)redis任务 workflow系列教程(3)Series串联任务流…

Boto3按名字搜索AWS Image并返回Image的相关参数 (Python)

文章目录 小结问题及解决参考 小结 本文记录使用Python脚本和Boto3按名字搜索AWS Image并返回AWS Image的相关参数。 问题及解决 记得操作之前拿到相应的权限&#xff1a; export AWS_ACCESS_KEY_ID"xxxxxxxxxxxxxxxxxxxxxxxxxx"export AWS_SECRET_ACCESS_KEY&qu…

《Linux C编程实战》笔记:进程操作之ID,优先级

获得进程ID getpid函数 这个函数都用了很多次了&#xff0c;看一下定义和例子就行了 #include<sys/types.h> #include <unistd.h> pid_t getpid(void); 示例程序1 #include<cstdlib> #include<malloc.h> #include<cstring> #include <cs…

Tomcat (Linux系统)详解全集

点击标题进入对应模块学习&#xff0c;你也可以完全拿捏Tomcat&#xff01; 1 Tomcat及JDK下载安装&#xff08;Linux系统&#xff09; 2 Tomcat目录介绍 3 Tomcat的启动关闭及日志说明 4 完美解决Tomcat启动慢的三种方法 5 Tomcat管理功能使用 6 Tomcat主配置文件&#xff08;…

SSM整合实战(Spring、SpringMVC、MyBatis)

五、SSM整合实战 目录 一、SSM整合理解 1. 什么是SSM整合&#xff1f;2. SSM整合核心理解五连问&#xff01; 2.1 SSM整合涉及几个IoC容器&#xff1f;2.2 每个IoC容器盛放哪些组件&#xff1f;2.3 IoC容器之间是什么关系&#xff1f;2.4 需要几个配置文件和对应IoC容器关系&…