使程序脱离终端,完全放在后台运行代码:
void daemon(void) { int fr=0; fr = fork(); if( fr < 0 ) { fprintf(stderr, "fork() failed\n"); exit(1); } if ( fr > 0 ) { exit(0); } if( setsid() < 0 ) { fprintf(stderr, "setsid() failed\n"); exit(1); } fr = fork(); if( fr < 0 ) { fprintf(stderr, "fork() failed\n"); exit(1); } if ( fr > 0 ) { fprintf(stderr, "forked to background (%d)\n", fr); exit(0); } umask(0); chdir("/"); close(0); close(1); close(2); open("/dev/null", O_RDWR); dup(0); dup(0); }
时间: 2024-10-16 05:30:50