linux下使用c++创建守护进程

#include<stdio.h>                                                                                                                                  
#include<stdlib.h>
#include<string>
#include<iostream>
#include<fcntl.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<sys/stat.h>
using namespace std;
#define MAXFILE 65535
//实现一个守护进程:每隔5秒在/tmp/dameon.log中写入一句话
int main(){
    pid_t pc; 
    int i,fd,len;
    //char *buf="this is a Dameon\n";
    string buf="this is a Dameon\n";
    //len = strlen(buf);
    len=buf.size();
    pc = fork(); /*第一步:创建子进程*/
    if(pc<0){
        printf("error fork\n");
        exit(1);
    }else if(pc>0){//父进程退出
        exit(0);
    }  
    setsid(); /*第二步:在子进程中创建新会话*/
    char szPath[1024];
    if(getcwd(szPath, sizeof(szPath)) == NULL)
    {//获得当前路径
        perror("getcwd");
        exit(1);
    }  
    //printf("current working directory : %s\n", szPath);
    //chdir("/"); /*第三步:改变当前目录为根目录*/
    chdir(szPath);
    umask(0); /*第四步:重设文件权限掩码*/
    for(i=0;i<MAXFILE;i++) /*第五步:关闭文件描述符*/                                                                                               
        close(i);
    while(1){
        //if((fd=open("/tmp/dameon.txt",O_CREAT|O_WRONLY|O_APPEND,0600))<0){
        if((fd=open("dameon.txt",O_CREAT|O_WRONLY|O_APPEND,0600))<0){
            perror("open");
            exit(1);
        }   
        //write(fd,buf,len+1);
        write(fd,buf.c_str(),len);//写入文件
        close(fd);
        sleep(5);
    }  
    return 0;
}   

相关命令:

ps aux | grep 文件名
kill -9 进程ID
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值