├── .gitignore ├── example └── web.go ├── godaemon.go ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | -------------------------------------------------------------------------------- /example/web.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | _ "github.com/icattlecoder/godaemon" 5 | "log" 6 | "net/http" 7 | ) 8 | 9 | func main() { 10 | 11 | mux := http.NewServeMux() 12 | mux.HandleFunc("/index", func(rw http.ResponseWriter, req *http.Request) { 13 | rw.Write([]byte("hello, golang!\n")) 14 | }) 15 | log.Fatalln(http.ListenAndServe(":7070", mux)) 16 | } 17 | -------------------------------------------------------------------------------- /godaemon.go: -------------------------------------------------------------------------------- 1 | package godaemon 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "os" 7 | "os/exec" 8 | ) 9 | 10 | var godaemon = flag.Bool("d", false, "run app as a daemon with -d=true") 11 | 12 | func init() { 13 | if !flag.Parsed() { 14 | flag.Parse() 15 | } 16 | if *godaemon { 17 | args := os.Args[1:] 18 | i := 0 19 | for ; i < len(args); i++ { 20 | if args[i] == "-d" || args[i] == "-d=true" { 21 | args[i] = "-d=false" 22 | break 23 | } 24 | } 25 | cmd := exec.Command(os.Args[0], args...) 26 | cmd.Start() 27 | fmt.Println("[PID]", cmd.Process.Pid) 28 | os.Exit(0) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | godaemon 2 | ======== 3 | 4 | Run golang app as background program, 以后台形式运行golang 5 | 6 | ## 安装: 7 | 8 | ``` 9 | go get github.com/icattlecoder/godaemon 10 | ``` 11 | 12 | ## 示例: 13 | 14 | ```go 15 | package main 16 | 17 | import ( 18 | _ "github.com/icattlecoder/godaemon" 19 | "log" 20 | "net/http" 21 | ) 22 | 23 | func main() { 24 | mux := http.NewServeMux() 25 | mux.HandleFunc("/index", func(rw http.ResponseWriter, req *http.Request) { 26 | rw.Write([]byte("hello, golang!\n")) 27 | }) 28 | log.Fatalln(http.ListenAndServe(":7070", mux)) 29 | } 30 | ``` 31 | 32 | ## 运行 33 | 34 | ``` 35 | ./example -d=true 36 | ~$ curl http://127.0.0.1:7070/index 37 | hello, golang! 38 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 wangming 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. --------------------------------------------------------------------------------