{"id":10006806,"date":"2022-11-04T10:37:13","date_gmt":"2022-11-04T02:37:13","guid":{"rendered":"https:\/\/ityet.com\/?p=10006806"},"modified":"2022-11-04T10:37:13","modified_gmt":"2022-11-04T02:37:13","slug":"%e6%96%87%e7%9b%98rust-%e6%8a%8a%e7%a8%8b%e5%ba%8f%e4%bd%9c%e4%b8%ba%e5%ae%88%e6%8a%a4%e8%bf%9b%e7%a8%8b%e5%90%af%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006806","title":{"rendered":"\u6587\u76d8Rust &#8212; \u628a\u7a0b\u5e8f\u4f5c\u4e3a\u5b88\u62a4\u8fdb\u7a0b\u542f\u52a8"},"content":{"rendered":"<p>\u5f53\u6211\u4eec\u5199\u5b8c\u4e00\u4e2a\u670d\u52a1\u7aef\u7a0b\u5e8f\uff0c\u9700\u8981\u4e0a\u7ebf\u90e8\u7f72\u7684\u65f6\u5019\uff0c\u6216\u591a\u6216\u5c11\u90fd\u4f1a\u548c\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b88\u62a4\u8fdb\u7a0b\u6253\u4ea4\u9053\uff0c\u6bd5\u7adf\u8c01\u4e5f\u4e0d\u5e0c\u671bshell\u5173\u95ed\u65e2\u505c\u670d\u3002\u4eca\u5929\u6211\u4eec\u5c31\u6765\u804a\u804a\u8fd9\u4e2a\u4e8b\u513f\u3002<\/p>\n<p>\u6700\u65e9\u5927\u5bb6\u90e8\u7f72\u5e94\u7528\u7684\u901a\u5e38\u64cd\u4f5c\u662f \u201cnohup xxxx &#038;\u201d\uff0c\u522b\u8bf4\u50cfweblogic \u6216\u8005\u5176\u4ed6java \u5bb9\u5668\u6709\u542f\u52a8\u811a\u672c\uff0c\u91cc\u9762\u5176\u5b9e\u4e5f\u5dee\u4e0d\u591a\uff1b\u5f88\u559c\u6b22 nginx\u7684 -d \u53c2\u6570\uff0c\u6216\u8005\u50cfredis \u914d\u7f6e\u6587\u4ef6\u91cc\u53ef\u4ee5\u6307\u5b9a\u662f\u5426\u4ee5\u5b88\u62a4\u8fdb\u7a0b\u542f\u52a8\u3002\u770b\u8d77\u6765\u5f88\u4f18\u96c5\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u4f7f\u7528rust \u5199\u4e00\u4e2a\u670d\u52a1\u7aef\u7a0b\u5e8f\u80fd\u4e0d\u80fd\u4f18\u96c5\u7684\u4f7f\u7528\u4e00\u4e2a\u53c2\u6570\u6307\u5b9a\u5e94\u7528 daemon \u6a21\u5f0f\u542f\u52a8\uff0c\u540c\u65f6\u4f7f\u7528stop \u65b9\u5f0f\u4f18\u96c5\u7684\u505c\u673a\u5462\uff1f\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u8bf4\u57fa\u672c\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>\u5b9e\u4f8b\u4ee3\u7801\u4f9d\u7136\u96c6\u6210\u5728interactcli-rs\u5de5\u7a0b\u4e2d\u3002<\/p>\n<p>\u9996\u5148\u6765\u6a21\u62df\u4e00\u4e2a\u542f\u52a8\u7684\u670d\u52a1\u8fdb\u7a0b \/src\/server\/server.rs<\/p>\n<pre><code>pub fn start(prefix: String) {\n    for i in 0..1000 {\n        println!(&quot;{}&quot;, prefix.clone() + &amp;i.to_string());\n        thread::sleep(Duration::from_secs(1));\n    }\n}<\/code><\/pre>\n<p>\u7a0b\u5e8f\u6bcf\u79d2\u8f93\u51fa\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6301\u7eed999\u79d2\uff0c\u8fd9\u4e2a\u65f6\u95f4\u8db3\u591f\u9a8c\u8bc1\u5b9e\u9a8c\u7ed3\u679c\u4e86\u3002<\/p>\n<p>\u540e\u53f0\u542f\u52a8\u6709\u4e24\u4e2a\u5b9e\u73b0\uff0c\u5206\u522b\u662f\u5229\u7528fork \u6216 daemonize,\u8fd9\u4e24\u4e2acrate \u5b9e\u73b0\u539f\u7406\u7c7b\u4f3c\uff0c\u4f46\u5728\u4f7f\u7528\u4e0a\u7a0d\u6709\u4e0d\u540c\u3002<\/p>\n<p>\/src\/cmd\/cmdserver.rs,\u6784\u5efa\u4e86\u4e24\u4e2a\u542f\u52a8\u5b50\u547d\u4ee4\uff0c\u5206\u522b\u6765\u8c03\u7528 fork \u548c daemonize\u7684\u5b88\u62a4\u8fdb\u7a0b\u542f\u52a8\u5b9e\u73b0\u3002<\/p>\n<pre><code>pub fn new_server_cmd() -&gt; Command {\n    clap::Command::new(&quot;server&quot;)\n        .about(&quot;server&quot;)\n        .subcommand(server_start_byfork())\n        .subcommand(server_start_bydaemonize())\n}\n\npub fn server_start_byfork() -&gt; Command {\n    clap::Command::new(&quot;byfork&quot;)\n        .about(&quot;start daemon by fork crate&quot;)\n        .arg(\n            Arg::new(&quot;daemon&quot;)\n                .short('d')\n                .long(&quot;daemon&quot;)\n                .action(ArgAction::SetTrue)\n                .help(&quot;start as daemon&quot;)\n                .required(false),\n        )\n}\npub fn server_start_bydaemonize() -&gt; Command {\n    clap::Command::new(&quot;bydaemonize&quot;)\n        .about(&quot;start daemon by daemonize crate&quot;)\n        .arg(\n            Arg::new(&quot;daemon&quot;)\n                .short('d')\n                .long(&quot;daemon&quot;)\n                .action(ArgAction::SetTrue)\n                .help(&quot;start as daemon&quot;)\n                .required(false),\n        )\n}<\/code><\/pre>\n<p>server \u7684\u5b50\u547d\u4ee4 byfork \u542f\u52a8 \u901a\u8fc7 fork \u5b9e\u73b0\u7684\u529f\u80fd\uff0cbydaemonize \u5219\u8c03\u7528\u901a\u8fc7 daemonize \u7684\u529f\u80fd\u5b9e\u73b0\u3002<\/p>\n<p>\u547d\u4ee4\u89e3\u6790\u7684\u4ee3\u7801\u5728 \/src\/cmd\/rootcmd.rs \u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u5148\u6765\u770b\u770b\u57fa\u4e8e fork \u7684\u5b9e\u73b0<\/p>\n<pre><code>if let Some(startbyfork) = server.subcommand_matches(&quot;byfork&quot;) {\n    println!(&quot;start by fork&quot;);\n    if startbyfork.get_flag(&quot;daemon&quot;) {\n        let args: Vec&lt;String&gt; = env::args().collect();\n        if let Ok(Fork::Child) = daemon(true, false) {\n            \/\/ \u542f\u52a8\u5b50\u8fdb\u7a0b\n            let mut cmd = Command::new(&amp;args[0])\n            for idx in 1..args.len() {\n                let arg = args.get(idx).expect(&quot;get cmd arg error!&quot;);\n                \/\/ \u53bb\u9664\u540e\u53f0\u542f\u52a8\u53c2\u6570,\u907f\u514d\u91cd\u590d\u542f\u52a8\n                if arg.eq(&quot;-d&quot;) || arg.eq(&quot;-daemon&quot;) {\n                    continue;\n                }\n                cmd.arg(arg);\n            \n            let child = cmd.spawn().expect(&quot;Child process failed to start.&quot;);\n            fs::write(&quot;pid&quot;, child.id().to_string()).unwrap();\n            println!(&quot;process id is:{}&quot;, std::process::id());\n            println!(&quot;child id is:{}&quot;, child.id());\n        }\n        println!(&quot;{}&quot;, &quot;daemon mod&quot;);\n        process::exit(0);\n    }\n    start(&quot;by_fork:&quot;.to_string());\n}<\/code><\/pre>\n<p>\u9996\u5148\uff0c\u901a\u8fc7 Fork::daemon \u51fd\u6570\u6d3e\u751f\u51fa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff1b\u7136\u540e\u89e3\u6790\u4e00\u4e0b\u5f53\u524d\u547d\u4ee4\uff0c\u53bb\u6389 -d \u53c2\u6570\uff0c\u6784\u5efa\u4e00\u4e2a\u542f\u52a8\u547d\u4ee4\uff0c\u5b50\u547d\u4ee4\u542f\u52a8\uff0c\u9000\u51fa\u7236\u8fdb\u7a0b\u3002\u8fd9\u57fa\u672c\u7b26\u5408\u64cd\u4f5c\u7cfb\u7edf\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\u7684\u8fc7\u7a0b \u2013 \u4e24\u6b21 fork\u3002<\/p>\n<p>\u518d\u6765\u770b\u770b\u57fa\u4e8e daemonize \u7684\u5b9e\u73b0\u3002<\/p>\n<pre><code>if let Some(startbydaemonize) = server.subcommand_matches(&quot;bydaemonize&quot;) {\n            println!(&quot;start by daemonize&quot;);\n            let base_dir = env::current_dir().unwrap();\n            if startbydaemonize.get_flag(&quot;daemon&quot;) {\n                let stdout = File::create(&quot;\/tmp\/daemon.out&quot;).unwrap();\n                let stderr = File::create(&quot;\/tmp\/daemon.err&quot;).unwrap();\n\n                println!(&quot;{:?}&quot;, base_dir);\n\n                let daemonize = Daemonize::new()\n                    .pid_file(&quot;\/tmp\/test.pid&quot;) \/\/ Every method except `new` and `start`\n                    .chown_pid_file(true) \/\/ is optional, see `Daemonize` documentation\n                    .working_directory(base_dir.as_path()) \/\/ for default behaviour.          \n                    .umask(0o777) \/\/ Set umask, `0o027` by default.\n                    .stdout(stdout) \/\/ Redirect stdout to `\/tmp\/daemon.out`.\n                    .stderr(stderr) \/\/ Redirect stderr to `\/tmp\/daemon.err`.\n                    .privileged_action(|| &quot;Executed before drop privileges&quot;);\n\n                match daemonize.start() {\n                    Ok(_) =&gt; {\n                        println!(&quot;Success, daemonized&quot;);\n                    }\n                    Err(e) =&gt; eprintln!(&quot;Error, {}&quot;, e),\n                }\n            }\n            println!(&quot;pid is:{}&quot;, std::process::id());\n            fs::write(&quot;pid&quot;, process::id().to_string()).unwrap();\n            start(&quot;by_daemonize:&quot;.to_string());\n        }<\/code><\/pre>\n<p>\u9996\u5148\u83b7\u53d6\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b daemonize \u4f1a\u5c06\u5de5\u4f5c\u76ee\u5f55\u8bbe\u7f6e\u4e3a \u201c\/\u201d,\u4e3a\u4e86\u907f\u514d\u6743\u9650\u95ee\u9898\uff0c\u6211\u4eec\u83b7\u53d6\u5f53\u524d\u76ee\u5f55\u4f5c\u4e3a\u5b88\u62a4\u8fdb\u7a0b\u7684\u5de5\u4f5c\u76ee\u5f55\u3002\u4e0d\u77e5\u9053\u662f\u4ec0\u4e48\u539f\u56e0\uff0c\u5728\u914d\u7f6e\u4e86pid_file \u540e\uff0c\u542f\u52a8\u5b88\u62a4\u8fdb\u7a0b\u65f6\u5e76\u6ca1\u5728\u6587\u4ef6\u4e2d\u6709\u8bb0\u5f55 pid\u3002\u4e0d\u8fc7\u4e5f\u6ca1\u5173\u7cfb\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5916\u90e8\u83b7\u53d6\u5e76\u8bb0\u5f55\u5b88\u62a4\u8fdb\u7a0b\u7684pid\u3002<\/p>\n<p>\u4e24\u79cd\u65b9\u5f0f\u542f\u52a8\u7684\u5b88\u62a4\u8fdb\u7a0b\u5747\u53ef\u5728\u5173\u95edshell\u7684\u60c5\u51b5\u4e0b\u7ef4\u6301\u8fdb\u7a0b\u8fd0\u884c\u3002<\/p>\n<p>\u4ece\u5b9e\u73b0\u4e0a\u6765\u8bb2\uff0c\u4e0d\u8bba\u662f fork \u8fd8\u662f daemonize \u90fd\u662f \u901a\u8fc7unsafe \u65b9\u5f0f\u8c03\u7528\u4e86 libc api\uff0c\u7c7b unix \u7cfb\u7edf\u5927\u591a\u8dd1\u8d77\u6765\u6ca1\u95ee\u9898\uff0cwindows \u7cfb\u7edf\u4f5c\u8005\u6ca1\u6709\u9a8c\u8bc1\u3002<\/p>\n<p>\u672c\u671f\u5173\u4e8e\u5b88\u62a4\u8fdb\u7a0b\u7684\u8bdd\u9898\u5c31\u804a\u5230\u8fd9\u513f\u3002<\/p>\n<p>\u54b1\u4eec\u4e0b\u671f\u89c1\u3002<\/p>\n<p><strong>\u4f5c\u8005\uff1a\u8d3e\u4e16\u95fb<\/strong><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042725423\">https:\/\/segmentfault.com\/a\/1190000042725423<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u6211\u4eec\u5199\u5b8c\u4e00\u4e2a\u670d\u52a1\u7aef\u7a0b\u5e8f\uff0c\u9700\u8981\u4e0a\u7ebf\u90e8\u7f72\u7684\u65f6\u5019\uff0c\u6216\u591a\u6216\u5c11\u90fd\u4f1a\u548c\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b88\u62a4\u8fdb\u7a0b\u6253\u4ea4\u9053\uff0c\u6bd5\u7adf\u8c01\u4e5f\u4e0d\u5e0c\u671bshell [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006806","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006806"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006806\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}