Skip to content

Instantly share code, notes, and snippets.

@leoncamel
Created September 5, 2022 08:32
Show Gist options
  • Save leoncamel/c96f2339acba60dd183babbf9a6f7f96 to your computer and use it in GitHub Desktop.
Save leoncamel/c96f2339acba60dd183babbf9a6f7f96 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os/exec"
"time"
)
func main() {
cmd := exec.Command("sleep", "1024")
err := cmd.Start()
if err != nil {
fmt.Println("Failed to Start cmd.")
return
}
fmt.Printf("cmd pid= %d\n", cmd.Process.Pid)
// _ = cmd.Process.Release()
// If I press Ctrl-C here, the process of `/bin/sleep` will gone.
// $ go run .
// cmd pid= 2536
// ^Csignal: interrupt
// $ ps -ef | grep 2536
// xiaolin 2797 32610 0 16:25 pts/63 00:00:00 grep --color=auto 2536
//
// But, in case if we wait 10 seconds, the process of `/bin/sleep` will keep running, and it's parent-pid will become 1.
// $ go run .
// cmd pid= 2980
// $ ps -ef | grep 2980
// xiaolin 2980 1 0 16:25 pts/63 00:00:00 sleep 1024
// xiaolin 3327 32610 0 16:25 pts/63 00:00:00 grep --color=auto 2980
time.Sleep(time.Second * 10)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment