zoukankan      html  css  js  c++  java
  • C++ 后台进程 daemon

    bool Switcher::Daemon() {
      base::YamlConfig config;
      if (!config_.ReadConfig(config_file_)) {
        fprintf(stderr, "read config file fail.
    ");
        return false;
      }
      bool is_daemon = config_.Get<bool>(kService, "is_daemon");
      if (!is_daemon) {
        return true;
      }
      if (fork() != 0) {
        exit(0);
      }
      setsid();
      if (fork() != 0) {
        exit(0);
      }
    
      close(0);
      close(1);
      close(2);
      open("/dev/null", O_RDWR);
      dup(0);
      dup(0);
    
      std::string stdout_log_path;
      if(config_.Get<std::string>(kService, kStdoutLogPath, &stdout_log_path)) {
        string filename = stdout_log_path + "." + DateString_();
        int fd = open(filename.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644);
        if (fd != -1) {
          // 将STDOUT, STDERROR 重定向到文件
          dup2(fd, STDOUT_FILENO);
          dup2(fd, STDERR_FILENO);
          close(fd);
        }
      }
    
      return true;
    }
  • 相关阅读:
    POJ--3667 Hotel
    Dragon Balls
    Popular Cows
    Tunnel Warfare [HDU--1540]
    CompletableFuture
    Future<V>
    分布式架构知识体系
    异步I/O和非阻塞I/O(轮询)
    同步异步阻塞非阻塞及并发级别
    volatile
  • 原文地址:https://www.cnblogs.com/diegodu/p/9173241.html
Copyright © 2011-2022 走看看