github.com/thediveo/gons@v0.9.9/reexec/doc.go (about)

     1  /*
     2  Package reexec allows to fork and then re-execute the current application
     3  (process) in order to only invoke a specific action function.
     4  
     5  Why, because the Golang runtime sucks at fork() and switching Linux kernel
     6  mount namespaces. The go runtime spins up multiple threads, but Linux really
     7  doesn't like changing mount namespaces when a process has become
     8  multi-threaded.
     9  
    10  # Example
    11  
    12  The following example code registers an action called "action" to be run when
    13  forking and re-executing the example process. As early as possible in main()
    14  we check for a pending action using CheckAction(). It will either execute an
    15  action and then terminate the process, or return control flow in order to run
    16  the process as usual.
    17  
    18  The registered example action simply prints its result to os.Stdout and then
    19  returns, which immediately terminates the re-executed process. This result is
    20  returned to the parent process which initiated the re-execution.
    21  
    22  	import (
    23  	    "fmt"
    24  	    "os"
    25  	    "github.com/thediveo/gons/reexec"
    26  	}
    27  
    28  	func init() {
    29  	    reexec.Register("action", func() {
    30  	      fmt.Fprintln(os.Stdout, `"done"`)
    31  	    })
    32  	}
    33  
    34  	func main() {
    35  	    reexec.CheckAction()
    36  	    var result string
    37  	    _ = reexec.RunReexecAction(
    38  	      "action",
    39  	      reexec.Result(&result))
    40  	}
    41  
    42  Please note that reexec.RunReexecAction() optionally accepts the namespaces to
    43  run the action in, as well as a parameter and/or environment variables. The
    44  result is picked up in the variable specified using reexec.Result().
    45  */
    46  package reexec