mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
Add Replace The Current Process With An External Command as a go til.
This commit is contained in:
31
go/replace-the-current-process-with-an-external-command.md
Normal file
31
go/replace-the-current-process-with-an-external-command.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Replace The Current Process With An External Command
|
||||
|
||||
Go's `syscall.Exec` function can be used to execute an external program.
|
||||
Instead of forking a child process though, it runs the external command in
|
||||
place of the current process. You need to give the function three pieces of
|
||||
information: the location of the binary, the pieces of the command to be
|
||||
executed, and relevant environment. Here is a simple example.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import 'fmt'
|
||||
import 'os'
|
||||
import 'syscall'
|
||||
|
||||
func main() {
|
||||
// get the system's environment variables
|
||||
environment := os.Environ()
|
||||
|
||||
// get a slice of the pieces of the command
|
||||
command := []string{"tmux", "new-session", "-s", "burrito"}
|
||||
|
||||
err := syscall.Exec("/usr/local/bin/tmux", command, environment)
|
||||
if err != nil {
|
||||
fmt.Printf("%v", err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
When this program is executed, it will replace itself with a new tmux
|
||||
session named *burrito*.
|
||||
Reference in New Issue
Block a user