Golang Creating Daemons

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Package Main

Import (

"OS"

"Os/exec"

"Path/filepath"

)

If OS. Getppid ()!=1{//Determine if it is a child process, and the child process will be taken over by system 1th process when the parent process return

Filepath,_:=filepath. Abs (OS. ARGS[0])//Convert the execution file path in the command line argument to the available path
Cmd:=exec.command (Filepath,os. Args[1:] ...) Passing other commands into the generated process
Cmd. Stdin=os. Stdin//sets the file descriptor for the new process and can be redirected to the file
Cmd. Stdout=os. Stdout
Cmd. Stderr=os. Stderr
Cmd. Start ()//Begin execution of new process without waiting for new process to exit
Return

}

This allows the daemon to be created and the semaphore can be accepted

A different way

If OS. Getppid ()!=1{

Args:=append ([]string{filepath},os. Args[1:] ...)
Os. StartProcess (Filepath,args,&os. Procattr{files:[]*os. File{os. Stdin,os. Stdout,os. Stderr}})
Return

}

You can also create daemons, which say startprocess is the lower interface, and CMD is an advanced interface

Actually, it can be lower.

PID, _, Syserr: = Syscall. Rawsyscall (Syscall. Sys_fork, 0, 0, 0)
If Syserr! = 0 {
Utils.logerr (Syserr)

Return
}

The go source is actually creating a new process.

Call the system function fork directly to generate a new process, just so do not know why the semaphore processing function is always not registered

So you can do it in either of the two ways.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.