zoukankan      html  css  js  c++  java
  • C++ read and log linux command in physical file

    #include <iostream>
    #include <chrono>
    #include <thread>
    #include <functional>
    #include <uuid/uuid.h>
    #include <cstdlib>
    #include <fstream>
    
    using namespace std;
    
    static char *dtValue=(char*)malloc(20);
    static char *uuidValue = (char *)malloc(40);
    
    char *getTimeNow()
    {
        time_t rawTime = time(NULL);
        struct tm tmInfo = *localtime(&rawTime);
        strftime(dtValue, 20, "%Y%m%d%H%M%S", &tmInfo);
        return dtValue;
    }
    
    
    void executeLinxCommand();
    
    int main()
    {
        executeLinxCommand();
    }
    
    void executeLinxCommand()
    {
        string cmdText="ps -ef|grep h1 >test.txt";
        system(cmdText.c_str());
        fstream rFile("test.txt",ios::in);
        if(!rFile.is_open())
        {
            cout<<"Open test.txt failed!"<<endl;
            return;
        }
    
        string line;
        string key="./h1";
        while(getline(rFile,line))
        {
            if(line.find(key)!=std::string::npos)
            {
                cout<<"Key="<<key<<endl;
                break;
            }
        }
    
        cout<<"Finsihed in executeLinxCommand() and now is "<<getTimeNow()<<endl;
        free(dtValue);
        free(uuidValue);
    }

    Compile

    g++ -g h2.cpp -o h2 -lpthread -luuid

    Run ./h2

  • 相关阅读:
    增加新分类daily——“每天学了啥?”
    gcc选项中的编译过程
    一个带路径复制的perl脚本
    git
    mysql explain 详解
    CentOS Python2.4升级到Python2.7
    mongoDb查询
    linux php 扩展
    php-redis 扩展安装
    redis Linux的安装方法
  • 原文地址:https://www.cnblogs.com/Fred1987/p/15742481.html
Copyright © 2011-2022 走看看