我一直在尝试在Rust中创建一个简单的守护进程,该守护进程将使用tcp_stream侦听端口并输出消息。但是,我遇到了两个问题:
1)如果我的守护进程使用println !,它将崩溃。如果删除所有对println!的提及,则守护程序将运行。创建守护程序时stdout / stdin如何工作?
我在Rust邮件列表中找到的一个消息来源说:“对于现代的init系统,例如systemd或launchctl,这非常有效,应用程序开发人员不必关心守护进程,并且记录也可以仅通过stdout完成。” 这是什么意思?
2)当我在非守护程序模式下运行以下代码时,curl不会立即返回(运行类似$ curl -XPOST localhost:9337 -d 'hi')。我必须杀死curl才能让服务器打印某些内容。不会卷曲自动关闭连接吗?发送后的字节发送给服务器后,而不是关闭连接之后,对服务器不可用?
extern crate getopts;
use getopts::{optflag,getopts};
use std::io::Command;
use std::io::net::tcp::{TcpListener};
use std::io::{Acceptor,Listener};
use std::os;
fn main() {
let args: Vec<String> = os::args();
let opts = [
optflag("d", "daemon", "conver this into a daemon"),
];
let matches = match getopts(args.tail(), opts) {
Ok(m) => { m },
Err(f) => { fail!(f.to_string()) }
};
// Create a daemon? if necessary
if matches.opt_present("d") {
let child = Command::new(args[0].as_slice()) …Run Code Online (Sandbox Code Playgroud)