| 1 | godaemon |
| 2 | ======== |
| 3 | |
| 4 | Daemonize Go applications with `exec()` instead of `fork()`. Read our [blog post](https://vividcortex.com/blog/2013/08/27/godaemon-a-library-to-daemonize-go-apps/) on the subject. |
| 5 | |
| 6 | You can't daemonize the usual way in Go. Daemonizing is a Unix concept that requires |
| 7 | some [specific things](http://goo.gl/vTUsVy) you can't do |
| 8 | easily in Go. But you can still accomplish the same goals |
| 9 | if you don't mind that your program will start copies of itself |
| 10 | several times, as opposed to using `fork()` the way many programmers are accustomed to doing. |
| 11 | |
| 12 | It is somewhat controversial whether it's even a good idea to make programs daemonize themselves, |
| 13 | or how to do it correctly (and whether it's even possible to do correctly in Go). |
| 14 | Read [here](https://code.google.com/p/go/issues/detail?id=227), |
| 15 | [here](http://www.ryanday.net/2012/09/04/the-problem-with-a-golang-daemon/), |
| 16 | and [here](http://stackoverflow.com/questions/14537045/how-i-should-run-my-golang-process-in-background) |
| 17 | for more on this topic. However, at [VividCortex](https://vividcortex.com/) we do need to run one of our processes as a |
| 18 | daemon with the usual attributes of a daemon, and we chose the approach implemented in this package. |
| 19 | |
| 20 | Because of the factors mentioned in the first link just given, you should take great care when |
| 21 | using this package's approach. It works for us, because we don't do anything like starting up |
| 22 | goroutines in our `init()` functions, or other things that are perfectly legal in Go in general. |
| 23 | |
| 24 | ## Getting Started |
| 25 | |
| 26 | View the [package documentation](http://godoc.org/github.com/VividCortex/godaemon) |
| 27 | for details about how it works. Briefly, to make your program into a daemon, |
| 28 | do the following as soon as possible in your `main()` function: |
| 29 | |
| 30 | ```go |
| 31 | import ( |
| 32 | "github.com/VividCortex/godaemon" |
| 33 | ) |
| 34 | |
| 35 | func main() { |
| 36 | godaemon.MakeDaemon(&godaemon.DaemonAttr{}) |
| 37 | } |
| 38 | ``` |
| 39 | |
| 40 | Use the `CaptureOutput` attribute if you need to capture your program's |
| 41 | standard output and standard error streams. In that case, the function returns |
| 42 | two valid readers (`io.Reader`) that you can read from the program itself. |
| 43 | That's particularly useful for functions that write error or diagnosis messages |
| 44 | right to the error output, which are normally lost in a daemon. |
| 45 | |
| 46 | Use the `Files` attribute if you need to inherit open files into the daemon. |
| 47 | This is primarily intended for avoiding race conditions when holding locks on |
| 48 | those files (flocks). Releasing and re-acquiring locks between successive fork |
| 49 | calls opens up the chance for another program to steal the lock. However, by |
| 50 | declaring your file descriptors in the `Files` attribute, `MakeDaemon()` will |
| 51 | guarantee that locks are not released throughout the whole process. Your daemon |
| 52 | will inherit the file still holding the same locks, with no other process having |
| 53 | intervened in between. See the |
| 54 | [package documentation](http://godoc.org/github.com/VividCortex/godaemon) for |
| 55 | more details and sample code. (Note that you shouldn't use this feature to |
| 56 | inherit TTY descriptors; otherwise what you get is technically not a daemon.) |
| 57 | |
| 58 | |
| 59 | ## Contribute |
| 60 | |
| 61 | Contributions are welcome. Please open pull requests or issue reports! |
| 62 | |
| 63 | |
| 64 | ## License |
| 65 | |
| 66 | This repository is Copyright (c) 2013 VividCortex, Inc. All rights reserved. |
| 67 | It is licensed under the MIT license. Please see the LICENSE file for applicable |
| 68 | license terms. |
| 69 | |
| 70 | ## Authors |
| 71 | |
| 72 | The primary author is [Gustavo Kristic](https://github.com/gkristic), with some |
| 73 | documentation and other minor contributions by others at VividCortex. |
| 74 | |
| 75 | ## History |
| 76 | |
| 77 | An earlier version of this concept with a slightly different interface was |
| 78 | developed internally at VividCortex. |
| 79 | |
| 80 | ## Cats |
| 81 | |
| 82 | A Go Daemon is a good thing, and so we present an angelic cat picture: |
| 83 | |
| 84 |  |