Golang how the child process kills the processing method when using the ExecCommand call

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Recently in writing an application, the following is the code used, there are a lot of Internet users encounter this problem, the following is my solution, share.

Use the method, want to Exec.command the time use Setpgid set process group, kill the time use Killall kill all call generated process


Code implementation:

Linux processing methods:

Package Systemimport ("Syscall") func setpgid (PID, Pgid int) error {return syscall. Setpgid (PID, Pgid)}func getppids (PID int) ([]int, error) {return []int{}, Nil}func Kill (PIDs []uint32) {for _, pid: = Rang E PIDs {syscall. Kill (int (PID), Syscall. SIGKILL)}}func KillAll (PID int) error {return syscall. Kill (pid-(pid*2), Syscall. SIGKILL)}

Windows processing methods:

Package Systemimport ("OS" "Syscall" "unsafe") const (MAX_PATH = 260th32cs_snapprocess = 0x00000002) type Processinf o struct {Name stringpid uint32ppid uint32}type PROCESSENTRY32 struct {dwsize uint32cntusage uint 32th32processid uint32th32defaultheapid Uintptrth32moduleid uint32cntthreads Uint32th32parentproce SsID uint32pcpriclassbase int32dwflags uint32szexefile [Max_path]uint16}type HANDLE Uintptrvar (Modkernel32 = Syscall.) Newlazydll ("kernel32.dll") Proccreatetoolhelp32snapshot = Modkernel32. Newproc ("CreateToolhelp32Snapshot") Procprocess32first = Modkernel32. Newproc ("PROCESS32FIRSTW") Procprocess32next = Modkernel32. Newproc ("process32nextw") Procclosehandle = Modkernel32. Newproc ("CloseHandle")) func Setpgid (PID, Pgid int) error {return nil}func KillAll (PID int) Error {PIDs: = Getppids (UInt32 ( PID) Kill (PIDs) return Nil}func Kill (PIDs []uint32) {for _, pid: =Range PIDs {Pro, err: = OS. findprocess (int (PID)) if err! = Nil {Continue}pro. Kill ()}}func getppids (PID uint32) []uint32 {infos, err: = Getprocs () if err! = nil {return []uint32{pid}}var PIDs []uint32 = Make ([]uint32, 0, Len (infos)) var index int = 0pids = Append (PIDs, PID) var length int = len (PIDs) for index < length {f Or _, Info: = Range Infos {if info. PPid = = Pids[index] {PIDs = append (PIDs, info. Pid)}}index + = 1length = Len (PIDs)}return Pids}func Getprocs () (Procs []processinfo, err Error) {snap: = createtoolhelp32s Napshot (th32cs_snapprocess, UInt32 (0)) if Snap = = 0 {err = syscall. GetLastError () Return}defer CloseHandle (SNAP) var pe32 processentry32pe32.dwsize = UInt32 (unsafe. Sizeof (PE32)) If Process32First (snap, &pe32) = = False {err = Syscall. GetLastError () Return}procs = append (procs, Processinfo{syscall. Utf16tostring (PE32. SZEXEFILE[:260]), pe32. Th32processid, Pe32. TH32PARENTPROCESSID}) for Process32Next (snap, &pe32) {procs = append (procs, Processinfo{syscall. Utf16tostring (PE32. SZEXEFILE[:260]), pe32. Th32processid, Pe32. TH32PARENTPROCESSID})}return}func CreateToolhelp32Snapshot (Flags, processId UInt32) HANDLE {ret, _, _: = Proccreatetoolhelp32snapshot.call (UIntPtr (Flags), UIntPtr (processId)) if ret <= 0 {return HANDLE (0)}return HANDLE ( RET)}func Process32First (snapshot HANDLE, PE *processentry32) bool {ret, _, _: = Procprocess32first.call (UIntPtr ( snapshot), UIntPtr (unsafe. Pointer (PE))) return ret! = 0}func Process32Next (snapshot HANDLE, PE *processentry32) bool {ret, _, _: = Procprocess32next. Call (UIntPtr (snapshot), uintptr (unsafe. Pointer (PE))) return ret! = 0}func CloseHandle (object HANDLE) bool {ret, _, _: = Procclosehandle.call (UIntPtr (object)) Retu RN ret! = 0}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.