मैं अपने डेमॉन के साथ कांटा प्रक्रियाओं की कोशिश कर रहा हूं, और अपने डेमॉन दुर्घटनाओं के मामले में उन्हें अस्वीकार करने की कोशिश कर रहा हूं।गोलांग निष्पादन प्रक्रिया और इसे अस्वीकार करने के लिए
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
"time"
)
func main() {
cmd := "myproc"
binary, lookErr := exec.LookPath(cmd)
if lookErr != nil {
panic(lookErr)
}
fmt.Println(binary)
os.Remove("/tmp/stdin")
os.Remove("/tmp/stdout")
os.Remove("/tmp/stderr")
fstdin, err1 := os.Create("/tmp/stdin")
fstdout, err2 := os.Create("/tmp/stdout")
fstderr, err3 := os.Create("/tmp/stderr")
if err1 != nil || err2 != nil || err3 != nil {
fmt.Println(err1, err2, err3)
panic("WOW")
}
argv := []string{"hi"}
procAttr := syscall.ProcAttr{
Dir: "/tmp",
Files: []uintptr{fstdin.Fd(), fstdout.Fd(), fstderr.Fd()},
Env: []string{"VAR1=ABC123"},
Sys: &syscall.SysProcAttr{
Foreground: false,
},
}
pid, err := syscall.ForkExec(binary, argv, &procAttr)
fmt.Println("Spawned proc", pid, err)
time.Sleep(time.Second * 100)
}
मैं भी एक साधारण आवेदन है कि सोता है और प्रिंट हैलो दुनिया और यह पथ के लिए रखा बना दिया है: नियमित os/exec
उच्च स्तरीय, इसलिए मैं syscall.ForkExec
के लिए चला गया और निम्नलिखित कोड का उत्पादन किया है।
#include <stdio.h>
int main(){
while(1){
printf("hello world");
fflush(stdout);
usleep(300000);
}
}
यह काम करता है, हालांकि, प्रक्रिया को पृष्ठभूमि में नहीं भेजा जाता है जैसा कि मैंने उम्मीद की थी, मेरी जाने की प्रक्रिया अभी भी बच्चे का मालिक है। SysProcAttr
लिनक्स में निम्न मान है:
type SysProcAttr struct {
Chroot string // Chroot.
Credential *Credential // Credential.
Ptrace bool // Enable tracing.
Setsid bool // Create session.
Setpgid bool // Set process group ID to Pgid, or, if Pgid == 0, to new pid.
Setctty bool // Set controlling terminal to fd Ctty (only meaningful if Setsid is set)
Noctty bool // Detach fd 0 from controlling terminal
Ctty int // Controlling TTY fd
Foreground bool // Place child's process group in foreground. (Implies Setpgid. Uses Ctty as fd of controlling TTY)
Pgid int // Child's process group ID if Setpgid.
Pdeathsig Signal // Signal that the process will get when its parent dies (Linux only)
Cloneflags uintptr // Flags for clone calls (Linux only)
UidMappings []SysProcIDMap // User ID mappings for user namespaces.
GidMappings []SysProcIDMap // Group ID mappings for user namespaces.
// GidMappingsEnableSetgroups enabling setgroups syscall.
// If false, then setgroups syscall will be disabled for the child process.
// This parameter is no-op if GidMappings == nil. Otherwise for unprivileged
// users this should be set to false for mappings work.
GidMappingsEnableSetgroups bool
}
मैं भी कोशिश की निम्नलिखित लेकिन यह एक त्रुटि के कारण:
Sys: &syscall.SysProcAttr{
Setsid: true,
Setctty: true,
Foreground: false,
},
Spawned proc 0 inappropriate ioctl for device
इसके अलावा निम्नलिखित:
Sys: &syscall.SysProcAttr{
Setsid: true,
Setctty: true,
Foreground: false,
Noctty: true,
Setpgid: true,
},
Spawned proc 0 operation not permitted (with root privilleges)
मैं क्या कर रहा हूँ/गलत मानते हो? नोट: ओएस/exec उच्च स्तर के कहने के बावजूद, मैंने निम्नलिखित भी कोशिश की, लेकिन यह एक ही परिणाम उत्पन्न करता है।
cs := exec.Command(binary)
cs.SysProcAttr = &syscall.SysProcAttr{
Setctty: true,
}
err := cs.Run()
fmt.Println(err)
तुम्हें क्या लगता है 'exec.Cmd' भी उच्च स्तर है? आपके पास भी SysProcAttr तक पहुंच है। इसके अलावा, आपकी प्रक्रिया से आपका क्या मतलब है अभी भी बच्चे का मालिक है? क्या यह एक ही प्रक्रिया समूह में है? यदि माता-पिता बाहर निकलते हैं तो इसे पीआईडी 1 में फिर से सौंप दिया नहीं जाता है? – JimB