Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure go to start the program based on the port number and guard the process

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to configure go to start the program according to the port number and guard the process". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Func main () {fmt.Println ("- start checking whether the process is started every ten seconds -") c: = cron.New () c.AddFunc ("@ every 10s", portMonitoring) c.Start () select {}} func portMonitoring () {config: = commom.InitConfig (". / config/test1.txt") pidname: = config ["portOrPid"] arr:=strings.Split (pidname, " ") portname: = config [" portAndName "] portnameArr:=strings.Split (portname,", ") for iMagery item: = range arr {port, _: = strconv.Atoi (item) if portInUse (port) =-1 {println (" the program detected that the program did not start Start the application in 5 seconds ") fmt.Println (" the program is starting. ") Time.Sleep (time.Duration (5) * time.Second) autoStart (portnameArr [I])} else {fmt.Print ("process running! -Port number: ", port," run information: (cpu memory name creation time) * ") fmt.Println (getPidInfo (int32 (portInUse (port)}} func portInUse (portNumber int) int {res: =-1 var outBytes bytes.Buffer cmdStr: = fmt.Sprintf (" netstat-ano-p tcp | findstr% d ", portNumber) cmd: = exec.Command (" cmd "," / c ") CmdStr) cmd.Stdout = & outBytes cmd.Run () resStr: = outBytes.String () r: = regexp.MustCompile (`\ s\ d+\ s`) .FindAllString (resStr,-1) if len (r) > 0 {pid, err: = strconv.Atoi (strings.TrimSpace (r [0])) if err! = nil {res =-1} else {res = pid} return res} func getPidInfo (pid int32) (float64) Float32, string, int64) {var rootProcess * process.Process processes, _: = process.Processes () for _, p: = range processes {if p.Pid = = pid {rootProcess = p break}} info,_: = rootProcess.CPUPercent () / / cpu info2,_: = rootProcess.MemoryPercent () / / memory info3,_: = rootProcess.Name () / / name info4,_: = rootProcess.CreateTime () / / name return info,info2 Info3,info4} func autoStart (name string) {cmd: = exec.Command ("cmd.exe", "/ c", "start lnk/" + name) err: = cmd.Run () if err! = nil {fmt.Printf ("error:%v\ n", err)} else {fmt.Printf ("% s\ n", name)}}

/ / File directory

Config test1.txtlnk startup

/ / configuration file

/ / Application port number portOrPid=8989// application corresponds to shortcut portAndName=startup.lnk

/ / Multi-application monitoring, direct comma separation

/ / Monitoring tomcat,redisportOrPid=8989,6379portAndName=startup.lnk,redis-server.lnk

/ / configuration file read

/ / read the key=value type configuration file func InitConfig (path string) map [string] string {config: = make (map [string] string) f, err: = os.Open (path) defer f.Close () if err! = nil {panic (err)} r: = bufio.NewReader (f) for {b, _ Err: = r.ReadLine () if err! = nil {if err = = io.EOF {break} panic (err)} s: = strings.TrimSpace (string (b)) index: = strings.Index (s, "=") if index

< 0 { continue } key := strings.TrimSpace(s[:index]) if len(key) == 0 { continue } value := strings.TrimSpace(s[index+1:]) if len(value) == 0 { continue } config[key] = value } return config} //程序运行效果

This is the end of "how to configure go to start the program according to the port number and guard the process". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report