本文整理匯總了Golang中os.ProcAttr.Sys方法的典型用法代碼示例。如果您正苦於以下問題:Golang ProcAttr.Sys方法的具體用法?Golang ProcAttr.Sys怎麽用?Golang ProcAttr.Sys使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類os.ProcAttr
的用法示例。
在下文中一共展示了ProcAttr.Sys方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Execute
func (t *Task) Execute(arg0 string, argv []string, attr *os.ProcAttr) (*Status, error) {
t.Lock()
if jobControlEnabled() {
attr.Sys = SysProcAttr(t.Group)
}
proc, err := os.StartProcess(arg0, argv, attr)
if err != nil {
t.Unlock()
return nil, err
}
if jobControlEnabled() {
if t.Group == 0 {
t.Group = proc.Pid
}
}
t.pid = proc.Pid
t.Unlock()
status := JoinProcess(proc)
if jobControlEnabled() {
if t.Group == t.pid {
t.Group = 0
}
}
t.pid = 0
return NewStatus(int64(status)), err
}
示例2: Restart
func Restart() {
var attr os.ProcAttr
attr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
attr.Sys = &syscall.SysProcAttr{}
_, err := os.StartProcess(os.Args[0], os.Args, &attr)
if err != nil {
_err(err)
}
Stop()
}
示例3: main
func main() {
//var php_process os.Process
path := GetCurrPath()
php_exe := path + "php-cgi.exe"
php_ini := path + "php.ini"
avg := []string{php_exe, "-b", "127.0.0.1:9090", "-c", php_ini}
if _, err := os.Stat(php_exe); err != nil {
return
}
for {
/*
cmd := exec.Command(php_exe, avg...)
cmd.Stdin = os.Stdin //給新進程設置文件描述符,可以重定向到文件中
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
//隱藏
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
err := cmd.Start()
*/
procAttr := new(os.ProcAttr)
procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
procAttr.Sys = &syscall.SysProcAttr{HideWindow: true}
php_process, err := os.StartProcess(php_exe, avg, procAttr)
if err != nil {
fmt.Println(err)
time.Sleep(time.Second * 10)
continue
}
php_process.Wait()
// err = cmd.Wait()
//fmt.Println("out")
time.Sleep(time.Second * 1)
}
}
示例4: MakeDaemon
//.........這裏部分代碼省略.........
fileCount := 3 + len(attrs.Files)
files := make([]*os.File, fileCount, fileCount+2)
if stage == 0 {
// Descriptors 0, 1 and 2 are fixed in the "os" package. If we close
// them, the process may choose to open something else there, with bad
// consequences if some write to os.Stdout or os.Stderr follows (even
// from Go's library itself, through the default log package). We thus
// reserve these descriptors to avoid that.
nullDev, err := os.OpenFile("/dev/null", 0, 0)
if err != nil {
return fatal(err)
}
files[0], files[1], files[2] = nullDev, nullDev, nullDev
fd := 3
for _, fPtr := range attrs.Files {
files[fd] = *fPtr
saveFileName(fd, (*fPtr).Name())
fd++
}
} else {
files[0], files[1], files[2] = os.Stdin, os.Stdout, os.Stderr
fd := 3
for _, fPtr := range attrs.Files {
*fPtr = os.NewFile(uintptr(fd), getFileName(fd))
syscall.CloseOnExec(fd)
files[fd] = *fPtr
fd++
}
}
if stage < 2 {
// getExecutablePath() is OS-specific.
procName, err := GetExecutablePath()
if err != nil {
return fatal(fmt.Errorf("can't determine full path to executable: %s", err))
}
// If getExecutablePath() returns "" but no error, determinating the
// executable path is not implemented on the host OS, so daemonization
// is not supported.
if len(procName) == 0 {
return fatal(fmt.Errorf("can't determine full path to executable"))
}
if stage == 1 && attrs.CaptureOutput {
files = files[:fileCount+2]
// stdout: write at fd:1, read at fd:fileCount
if files[fileCount], files[1], err = os.Pipe(); err != nil {
return fatal(err)
}
// stderr: write at fd:2, read at fd:fileCount+1
if files[fileCount+1], files[2], err = os.Pipe(); err != nil {
return fatal(err)
}
}
if err := advanceStage(); err != nil {
return fatal(err)
}
dir, _ := os.Getwd()
osAttrs := os.ProcAttr{Dir: dir, Env: os.Environ(), Files: files}
if stage == 0 {
sysattrs := syscall.SysProcAttr{Setsid: true}
osAttrs.Sys = &sysattrs
}
progName := attrs.ProgramName
if len(progName) == 0 {
progName = os.Args[0]
}
args := append([]string{progName}, os.Args[1:]...)
proc, err := os.StartProcess(procName, args, &osAttrs)
if err != nil {
return fatal(fmt.Errorf("can't create process %s: %s", procName, err))
}
proc.Release()
os.Exit(0)
}
os.Chdir("/")
syscall.Umask(0)
resetEnv()
for fd := 3; fd < fileCount; fd++ {
resetFileName(fd)
}
currStage = DaemonStage(stage)
var stdout, stderr *os.File
if attrs.CaptureOutput {
stdout = os.NewFile(uintptr(fileCount), "stdout")
stderr = os.NewFile(uintptr(fileCount+1), "stderr")
}
return stdout, stderr, nil
}