{"id":3113,"date":"2017-01-06T19:28:57","date_gmt":"2017-01-06T11:28:57","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=3113"},"modified":"2017-01-06T19:28:57","modified_gmt":"2017-01-06T11:28:57","slug":"linux%e4%b8%8b%e5%ae%88%e6%8a%a4%e8%bf%9b%e7%a8%8b%e7%9a%84golang%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3113.html","title":{"rendered":"Linux\u4e0b\u5b88\u62a4\u8fdb\u7a0b\u7684Golang\u8bed\u8a00\u5b9e\u73b0"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>\u4ee5\u540e\u53ef\u80fd\u8981\u7528Golang\u8bed\u8a00\u5199\u4e00\u4e2adaemon\u7a0b\u5e8f\uff08\u5b88\u62a4\u8fdb\u7a0b\uff09\u8fd0\u884c\u5728\u670d\u52a1\u5668\u4e0a\uff0c\u4ee5\u8fdb\u884c\u4e00\u4e9b\u4fe1\u606f\u6536\u96c6\u7684\u5de5\u4f5c\u3002\u4e4b\u524d\u53ea\u662f\u7b80\u5355\u77e5\u9053\u4ec0\u4e48\u662f\u5b88\u62a4\u8fdb\u7a0b\uff0c\u4f46\u5bf9\u4e8e\u4e00\u4e9b\u7ec6\u8282\u4ee5\u53ca\u8be5\u5982\u4f55\u7528Golang\u8fdb\u884c\u7f16\u5199\u90fd\u4e0d\u592a\u6e05\u695a\uff0c\u6240\u4ee5\u9700\u8981\u5148\u5b66\u4e60\u3001\u51c6\u5907\u4e00\u4e0b\u3002<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>\u2026\u2026\u5f85\u8865\u5145\u2026\u2026<\/p>\n<pre class=\"lang:default decode:true \">package main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"os\"\n    \"os\/signal\"\n    \"runtime\"\n    \"syscall\"\n    \"time\"\n)\n\nfunc daemon(nochdir, noclose int) int {\n    var ret, ret2 uintptr\n    var err syscall.Errno\n    darwin := runtime.GOOS == \"darwin\"\n    \/\/ already a daemon\n    if syscall.Getppid() == 1 {\n        return 0\n    }\n    \/\/ fork off the parent process\n    ret, ret2, err = syscall.RawSyscall(syscall.SYS_FORK, 0, 0, 0)\n    if err != 0 {\n        return -1\n    }\n    \/\/ failure\n    if ret2 &lt; 0 {\n        os.Exit(-1)\n    }\n    \/\/ handle exception for darwin\n    if darwin &amp;&amp; ret2 == 1 {\n        ret = 0\n    }\n    \/\/ if we got a good PID, then we call exit the parent process.\n    if ret &gt; 0 {\n        os.Exit(0)\n    }\n    \/* Change the file mode mask *\/\n    _ = syscall.Umask(0)\n\n    \/\/ create a new SID for the child process\n    s_ret, s_errno := syscall.Setsid()\n    if s_errno != nil {\n        log.Printf(\"Error: syscall.Setsid errno: %d\", s_errno)\n    }\n    if s_ret &lt; 0 {\n        return -1\n    }\n    if nochdir == 0 {\n        os.Chdir(\"\/\")\n    }\n    if noclose == 0 {\n        f, e := os.OpenFile(\"\/dev\/null\", os.O_RDWR, 0)\n        if e == nil {\n            fd := f.Fd()\n            syscall.Dup2(int(fd), int(os.Stdin.Fd()))\n            syscall.Dup2(int(fd), int(os.Stdout.Fd()))\n            syscall.Dup2(int(fd), int(os.Stderr.Fd()))\n        }\n    }\n    return 0\n}\n\nfunc main() {\n    daemon(0, 1)\n    for {\n        fmt.Println(\"hello\")\n        time.Sleep(1 * time.Second)\n    }\n}<\/pre>\n<p>\u2026\u2026\u5f85\u8865\u5145\u2026\u2026<\/p>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li>Go\u5230\u76ee\u524d\u8fd8\u6ca1\u6709\u89e3\u51b3\u6210\u4e3a\u5b88\u62a4\u8fdb\u7a0b(Daemonize)\u7684\u95ee\u9898\u5427\uff1f\u5404\u4f4d\u662f\u600e\u4e48\u89e3\u51b3\u7684\uff1f<br \/>\n<a href=\"https:\/\/segmentfault.com\/q\/1010000000699471\">https:\/\/segmentfault.com\/q\/1010000000699471<\/a><\/li>\n<li>Golang\u521b\u5efadaemon\u7a0b\u5e8f<br \/>\n<a href=\"http:\/\/shanks.leanote.com\/post\/Go%E5%88%9B%E5%BB%BAdaemon%E7%A8%8B%E5%BA%8F\">http:\/\/shanks.leanote.com\/post\/Go%E5%88%9B%E5%BB%BAdaemon%E7%A8%8B%E5%BA%8F<\/a><\/li>\n<li>Go\u8bed\u8a00daemon\u542f\u52a8\u7684\u89e3\u51b3\u65b9\u6cd5.linux\u5e73\u53f0<br \/>\n<a href=\"http:\/\/blog.csdn.net\/fyxichen\/article\/details\/50541449\">http:\/\/blog.csdn.net\/fyxichen\/article\/details\/50541449<\/a><\/li>\n<li>How to create a daemon process in Golang?<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/23736046\/how-to-create-a-daemon-process-in-golang\">http:\/\/stackoverflow.com\/questions\/23736046\/how-to-create-a-daemon-process-in-golang<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/sevlyar\/go-daemon\">https:\/\/github.com\/sevlyar\/go-daemon<\/a><br \/>\n<a href=\"https:\/\/github.com\/takama\/daemon\">https:\/\/github.com\/takama\/daemon<\/a><br \/>\n<a href=\"https:\/\/github.com\/VividCortex\/godaemon\">https:\/\/github.com\/VividCortex\/godaemon<\/a><br \/>\n<a href=\"https:\/\/github.com\/fiorix\/go-daemon\">https:\/\/github.com\/fiorix\/go-daemon<\/a><\/li>\n<li>=<\/li>\n<li>How do I get my Golang web server to run in the background?<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/12486691\/how-do-i-get-my-golang-web-server-to-run-in-the-background\">http:\/\/stackoverflow.com\/questions\/12486691\/how-do-i-get-my-golang-web-server-to-run-in-the-background<\/a><\/li>\n<li>supervisor\u8fd0\u884cgolang\u5b88\u62a4\u8fdb\u7a0b<br \/>\n<a href=\"http:\/\/www.01happy.com\/supervisor-golang-daemon\/\">http:\/\/www.01happy.com\/supervisor-golang-daemon\/<\/a><\/li>\n<li>\u7528golang\u542f\u52a8\u4e00\u4e2adaemon<br \/>\n<a href=\"http:\/\/www.cnblogs.com\/toby\/p\/3262516.html\">http:\/\/www.cnblogs.com\/toby\/p\/3262516.html<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u4ee5\u540e\u53ef\u80fd\u8981\u7528Golang\u8bed\u8a00\u5199\u4e00\u4e2adaemon\u7a0b\u5e8f\uff08\u5b88\u62a4\u8fdb\u7a0b\uff09\u8fd0\u884c\u5728\u670d\u52a1\u5668\u4e0a\uff0c\u4ee5\u8fdb\u884c\u4e00 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[717,564],"class_list":["post-3113","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-daemon","tag-golang"],"views":7284,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3113"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3113\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}