How to gracefully shut down child processes in Golang
Golang is a powerful programming language that supports multi-process programming. Starting a child process in Go is easy, but closing it requires some tricks. This article will introduce how to gracefully shut down child processes in Golang.
1. Starting a subprocess
Starting a subprocess in Golang is very simple. You can use the Command function in the exec package. The following is an example of starting a command:
cmd := exec.Command("ls", "-l") err := cmd.Start() if err != nil { log.Fatal(err) }
This code will start a child process of the "ls -l" command.
2. Close the child process
When the child process is completed, we need to call the Wait function to wait for the child process to close. However, if we are starting the child process in an infinite loop, then some more advanced tricks are needed to shut down the child process.
First, we need to store the PID of the child process in a variable. This can be achieved using the Getpid function in the syscall package. Then, when we need to shut down the child process, we can use the Kill function in the syscall package to send an operating system signal.
package main import ( "fmt" "os" "os/exec" "syscall" "time" ) func main() { cmd := exec.Command("sleep", "10") err := cmd.Start() if err != nil { fmt.Println(err) os.Exit(1) } pid := cmd.Process.Pid fmt.Printf("Child process started: %v\n", pid) time.AfterFunc(time.Second*5, func() { err := syscall.Kill(pid, syscall.SIGTERM) if err != nil { fmt.Println(err) } else { fmt.Printf("Child process %v terminated\n", pid) } }) cmd.Wait() fmt.Println("Parent process exiting.") }
This program will start a child process with the "sleep 10" command, then wait 5 seconds and then send the SIGTERM signal to kill the child process. We use the time.AfterFunc function to perform the operation after 5 seconds and the syscall.Kill function to send the signal. After completing the operation, we use the cmd.Wait function to wait for the child process to close.
3. Gracefully shut down the child process
In the above example, we sent a SIGTERM signal to shut down the child process. This signal will be caught by the process, and the process can do some cleanup work after catching the signal, and then exit normally.
If the child process does not catch the signal, we need to use the SIGKILL signal to force the child process to close. This signal will kill the process directly without giving the process any chance to clean up. Forcibly closing a child process may cause the process to leave temporary files and occupy system resources.
Gracefully shutting down a child process can be achieved by sending an exit signal to the child process. This signal can be caught by a child process and the process safely shut down after catching.
To use the exit signal, we need to support the signal in the code of the child process. In Golang, this can be achieved through the Signal function in the os package. Here is an example of a subprocess that supports exit signals:
package main import ( "fmt" "os" "os/signal" "syscall" "time" ) func main() { fmt.Println("Child process started.") sigchan := make(chan os.Signal, 1) signal.Notify(sigchan, syscall.SIGTERM) go func() { <-sigchan fmt.Println("Received SIGTERM signal, exiting...") os.Exit(0) }() // Start doing some meaningful work. for { fmt.Println("Working...") time.Sleep(time.Second) } }
This program starts an infinite loop and prints "Working..." every second. In the code of the child process, we create a signal channel sigchan and use the signal.Notify function to send the SIGTERM signal to the channel.
We then wait in an infinite loop to receive the signal. Once this signal is received, we print an exit message and terminate the process using the os.Exit function.
Now, we already have a subprocess that supports exit signals. We can use the following code to close it:
package main import ( "fmt" "os" "os/exec" "syscall" "time" ) func main() { cmd := exec.Command("./child") err := cmd.Start() if err != nil { fmt.Println(err) os.Exit(1) } pid := cmd.Process.Pid fmt.Printf("Child process started: %v\n", pid) time.AfterFunc(time.Second*5, func() { err := syscall.Kill(pid, syscall.SIGTERM) if err != nil { fmt.Println(err) } else { fmt.Printf("Child process %v terminated\n", pid) } }) cmd.Wait() fmt.Println("Parent process exiting.") }
This program will start a subprocess of the test1 command and wait for 5 seconds. Then send the SIGTERM signal to close the child process. Under normal circumstances, the child process should print an exit message and exit normally.
To sum up, closing the child process is not very difficult in Golang. We can use the Kill function in the syscall package to send signals, or support exit signals in the code of the child process to gracefully shut down the child process.
The above is the detailed content of How to gracefully shut down child processes in Golang. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

The article explains how to use the pprof tool for analyzing Go performance, including enabling profiling, collecting data, and identifying common bottlenecks like CPU and memory issues.Character count: 159

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

This article introduces a variety of methods and tools to monitor PostgreSQL databases under the Debian system, helping you to fully grasp database performance monitoring. 1. Use PostgreSQL to build-in monitoring view PostgreSQL itself provides multiple views for monitoring database activities: pg_stat_activity: displays database activities in real time, including connections, queries, transactions and other information. pg_stat_replication: Monitors replication status, especially suitable for stream replication clusters. pg_stat_database: Provides database statistics, such as database size, transaction commit/rollback times and other key indicators. 2. Use log analysis tool pgBadg

The article discusses the go fmt command in Go programming, which formats code to adhere to official style guidelines. It highlights the importance of go fmt for maintaining code consistency, readability, and reducing style debates. Best practices fo

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...
