Golang processes: Difference between revisions

From wikinotes
Line 33: Line 33:
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
os.Process.FindProcess()
import "syscall"
os.Process.Kill()
 
os.Process.Signal()
proc := os.Process.FindProcess(1234) // find process with pid
proc.Kill()                          // kill process (-9/SIGKILL)
proc.Signal(os.Iterrupt)              // send SIGINT to process (looks like you can use any syscall.SIG*)
 
syscall.Kill(1234, syscall.SIGINT)   // send SIGINT to pid 1234
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Manage Processes -->
</blockquote><!-- Manage Processes -->

Revision as of 04:13, 20 June 2022

Documentation

os https://pkg.go.dev/os@go1.18.3

Current Process

import "os"
import "os/user"

os.Executable()
os.Getpid()
os.Getppid()
os.Getuid()
os.Getgid()

os.Exit(1)              // exit process with exitcode '1'

// environment
envvars := os.Environ() // ["FOO=bar", "BAR=baz", ...]
pwd, err := os.Getwd()  // get current pwd/cwd
user.Current()          // 'will'

Manage Processes

import "syscall"

proc := os.Process.FindProcess(1234)  // find process with pid
proc.Kill()                           // kill process (-9/SIGKILL)
proc.Signal(os.Iterrupt)              // send SIGINT to process (looks like you can use any syscall.SIG*)

syscall.Kill(1234, syscall.SIGINT)    // send SIGINT to pid 1234

Subprocess

os.Process.StartProcess

ProcAttr
Process

Process Info