孟令鑫 1 år sedan
förälder
incheckning
88f9a5a0d1
3 ändrade filer med 36 tillägg och 24 borttagningar
  1. 24 6
      common/util/u_exec.go
  2. 6 9
      kinglong/common/svc/rosbag_record.go
  3. 6 9
      pji/common/svc/rosbag_record.go

+ 24 - 6
common/util/u_exec.go

@@ -1,16 +1,25 @@
 package util
 
 import (
+	"os"
 	"os/exec"
+	"strconv"
+	"strings"
 )
 
-func GetSubProcessPid(pid string) (string, error) {
-	pgrepCmd := exec.Command("pgrep", "-P", pid)
+// GetSubProcessPid 只用于一个子进程
+func GetSubProcessPid(pid int) (int, error) {
+	pgrepCmd := exec.Command("pgrep", "-P", strconv.Itoa(pid))
 	output, err := pgrepCmd.CombinedOutput()
 	if err != nil {
-		return "", err
+		return 0, err
 	}
-	return string(output), nil
+	replace := strings.Replace(string(output), "\n", "", 1)
+	subProcessPid, err := strconv.Atoi(replace)
+	if err != nil {
+		return 0, err
+	}
+	return subProcessPid, nil
 }
 
 func ExecuteWithPath(path string, name string, arg ...string) (*exec.Cmd, error) {
@@ -28,8 +37,17 @@ func ExecuteWithPath(path string, name string, arg ...string) (*exec.Cmd, error)
 
 }
 
-func KillProcessByPID(pid string) (*exec.Cmd, string, error) {
-	return Execute("kill", "-9", pid)
+// KillProcessByPid 必须使用该golang原生方法,通过直接执行kill命令行不通。
+func KillProcessByPid(pid int) error {
+	process, err := os.FindProcess(pid)
+	if err != nil {
+		return err
+	}
+	err = process.Kill()
+	if err != nil {
+		return err
+	}
+	return nil
 }
 
 func Execute(name string, arg ...string) (*exec.Cmd, string, error) {

+ 6 - 9
kinglong/common/svc/rosbag_record.go

@@ -6,7 +6,6 @@ import (
 	"cicv-data-closedloop/kinglong/common/cutil"
 	"cicv-data-closedloop/kinglong/common/log"
 	"github.com/bluenviron/goroslib/v2"
-	"strconv"
 	"time"
 )
 
@@ -45,8 +44,8 @@ func BagRecord(nodeName string) {
 		}
 		log.GlobalLogger.Info("启动record命令成功。")
 
-		recordProcessPid := strconv.Itoa(cmd.Process.Pid)
-		var recordSubProcessPid string
+		recordProcessPid := cmd.Process.Pid
+		var recordSubProcessPid int
 		for {
 			time.Sleep(time.Duration(2) * time.Second)
 			recordSubProcessPid, err = util.GetSubProcessPid(recordProcessPid)
@@ -54,7 +53,7 @@ func BagRecord(nodeName string) {
 				log.GlobalLogger.Info("正在等待获取进程 ", recordProcessPid, " 的子进程的pid。")
 				continue
 			}
-			if recordSubProcessPid != "" {
+			if recordSubProcessPid != 0 {
 				log.GlobalLogger.Info("获取进程 ", recordProcessPid, " 的子进程的pid:", recordSubProcessPid)
 				break
 			}
@@ -64,13 +63,11 @@ func BagRecord(nodeName string) {
 		select {
 		case signal := <-ChannelKillRosRecord:
 			if signal == 1 {
-				_, output, err := util.KillProcessByPID(recordSubProcessPid)
-				if err != nil {
-					log.GlobalLogger.Errorf("程序阻塞,杀死record命令子进程出错,【pid】=%v,【output】=%v,【err】=%v。", recordSubProcessPid, output, err)
+				if err = util.KillProcessByPid(recordSubProcessPid); err != nil {
+					log.GlobalLogger.Errorf("程序阻塞,杀死record命令子进程出错,【pid】=%v,【err】=%v。", recordSubProcessPid, err)
 					select {} // 此处阻塞防止record命令一直录包占满存储
 				}
-				err = cmd.Process.Kill()
-				if err != nil {
+				if err = cmd.Process.Kill(); err != nil {
 					log.GlobalLogger.Error("程序阻塞,杀死record命令父进程", recordProcessPid, "出错:", err)
 					select {} // 此处阻塞防止record命令一直录包占满存储
 				}

+ 6 - 9
pji/common/svc/rosbag_record.go

@@ -6,7 +6,6 @@ import (
 	"cicv-data-closedloop/pji/common/cfg"
 	"cicv-data-closedloop/pji/common/cutil"
 	"github.com/bluenviron/goroslib/v2"
-	"strconv"
 	"time"
 )
 
@@ -44,8 +43,8 @@ func BagRecord(nodeName string) {
 			continue
 		}
 		c_log.GlobalLogger.Info("启动record命令成功。")
-		recordProcessPid := strconv.Itoa(cmd.Process.Pid)
-		var recordSubProcessPid string
+		recordProcessPid := cmd.Process.Pid
+		var recordSubProcessPid int
 		for {
 			time.Sleep(time.Duration(2) * time.Second)
 			recordSubProcessPid, err = util.GetSubProcessPid(recordProcessPid)
@@ -53,7 +52,7 @@ func BagRecord(nodeName string) {
 				c_log.GlobalLogger.Info("正在等待获取进程 ", recordProcessPid, " 的子进程的pid。")
 				continue
 			}
-			if recordSubProcessPid != "" {
+			if recordSubProcessPid != 0 {
 				c_log.GlobalLogger.Info("获取进程 ", recordProcessPid, " 的子进程的pid:", recordSubProcessPid)
 				break
 			}
@@ -63,13 +62,11 @@ func BagRecord(nodeName string) {
 		select {
 		case signal := <-ChannelKillRosRecord:
 			if signal == 1 {
-				_, output, err := util.KillProcessByPID(recordSubProcessPid)
-				if err != nil {
-					c_log.GlobalLogger.Errorf("程序阻塞,杀死record命令子进程出错,【pid】=%v,【output】=%v,【err】=%v。", recordSubProcessPid, output, err)
+				if err := util.KillProcessByPid(recordSubProcessPid); err != nil {
+					c_log.GlobalLogger.Errorf("程序阻塞,杀死record命令子进程出错,【pid】=%v,【err】=%v。", recordSubProcessPid, err)
 					select {} // 此处阻塞防止record命令一直录包占满存储
 				}
-				err = cmd.Process.Kill()
-				if err != nil {
+				if err = cmd.Process.Kill(); err != nil {
 					c_log.GlobalLogger.Error("程序阻塞,杀死record命令进程", recordProcessPid, "出错:", err)
 					select {} // 此处阻塞防止record命令一直录包占满存储
 				}