Run Command in Golang and Detach It from Process
This problem arises when you need to execute a long-running process in Golang, with specific requirements such as:
Your attempted solution using Cmd.Start() and Cmd.Wait() fulfills most of these requirements except for preventing the underlying process from crashing when you send SIGTERM/SIGKILL to your program.
Solution
Instead of attempting to reinvent the wheel, consider utilizing one of the following libraries:
Go-reap Library
A recommended solution is to use the https://github.com/hashicorp/go-reap library.
Notes
Disclaimer
The provided solution focuses on addressing the issue of preventing the sub-process from becoming a zombie. For a complete solution that addresses all the requirements, a combination of approaches may be necessary.
The above is the detailed content of How to Execute Long-Running Processes in Golang and Prevent Them From Becoming Zombies?. For more information, please follow other related articles on the PHP Chinese website!