{"id":4603,"date":"2022-01-29T17:38:59","date_gmt":"2022-01-29T08:38:59","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4603"},"modified":"2022-01-29T21:55:46","modified_gmt":"2022-01-29T12:55:46","slug":"linux-deamon-%ed%94%84%eb%a1%9c%ea%b7%b8%eb%9e%a8-%ec%83%9d%ec%84%b1%ed%95%98%ea%b8%b0","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4603","title":{"rendered":"Linux daemon \ud504\ub85c\uadf8\ub7a8 \uc0dd\uc131\ud558\uae30"},"content":{"rendered":"<h1>Linux daemon \ud504\ub85c\uadf8\ub7a8 \uc0dd\uc131\ud558\uae30<\/h1>\n<p><a href=\"http:\/\/www.netzmafia.de\/skripten\/unix\/linux-daemon-howto.html\">\ucc38\uc870<\/a><\/p>\n<p>. . . . . . . . . . . . . . . . . . . . . .<br \/>\n. . . . . . . . . . . . . . . . . . . . . .<br \/>\n. . . . . . . . . . . . . . . . . . . . . .<\/p>\n<h2>\uc18c\uc2a4\ucf54\ub4dc<\/h2>\n<pre><code class=\"language-cpp\">#include &lt;sys\/types.h&gt;\n#include &lt;sys\/stat.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;fcntl.h&gt;\n#include &lt;errno.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;syslog.h&gt;\n#include &lt;string.h&gt;\n\nint main(void) {\n\n        \/* Our process ID and Session ID *\/\n        pid_t pid, sid;\n\n        \/* Fork off the parent process *\/\n        pid = fork();\n        if (pid &lt; 0) {\n                exit(EXIT_FAILURE);\n        }\n        \/* If we got a good PID, then\n           we can exit the parent process. *\/\n        if (pid &gt; 0) {\n                exit(EXIT_SUCCESS);\n        }\n\n        \/* Change the file mode mask *\/\n        umask(0);\n\n        \/* Open any logs here *\/\n\n        \/* Create a new SID for the child process *\/\n        sid = setsid();\n        if (sid &lt; 0) {\n                \/* Log the failure *\/\n                exit(EXIT_FAILURE);\n        }\n\n        \/* Change the current working directory *\/\n        if ((chdir(&quot;\/&quot;)) &lt; 0) {\n                \/* Log the failure *\/\n                exit(EXIT_FAILURE);\n        }\n\n        \/* Close out the standard file descriptors *\/\n        close(STDIN_FILENO);\n        close(STDOUT_FILENO);\n        close(STDERR_FILENO);\n\n        \/* Daemon-specific initialization goes here *\/\n\n        \/* The Big Loop *\/\n        while (1) {\n           \/* Do some task here ... *\/\n\n           sleep(30); \/* wait 30 seconds *\/\n        }\n   exit(EXIT_SUCCESS);\n}<\/code><\/pre>\n<h2>Makefile<\/h2>\n<p>vi Makefile<\/p>\n<pre><code class=\"language-Makefile\">AR=${CROSS_COMPILE}ar\nAS=${CROSS_COMPILE}as\nLD=${CROSS_COMPILE}ld\nCC=$(CROSS_COMPILE)gcc\nCXX=$(CROSS_COMPILE)g++\nNM=${CROSS_COMPILE}nm\nRANLIB=${CROSS_COMPILE}ranlib\n\nCFLAGS=&quot;&quot;\nCPPFLAGS=&quot;&quot;\nLDFLAGS=&quot;&quot;\nLIBS=&quot;&quot;\n\nhello : main.cpp\n        $(CXX) -g -o mydaemon main.cpp\n\nclean:\n        rm mydaemon<\/code><\/pre>\n<h2>\ucef4\ud30c\uc77c\ud558\uae30<\/h2>\n<pre><code class=\"language-bash\">make\n\n.\/mydaemon\n\nps -efH\n......\nskyer9   1943290       1  0 17:43 ?        00:00:00   .\/mydaemon\n\nkill 1943290<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Linux daemon \ud504\ub85c\uadf8\ub7a8 \uc0dd\uc131\ud558\uae30 \ucc38\uc870 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4603\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-4603","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4603"}],"version-history":[{"count":4,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4603\/revisions"}],"predecessor-version":[{"id":4610,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4603\/revisions\/4610"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}