Linux笔记:实现nohup功能

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

void closefd(void) {
	for(int i=0; i 0) {
		_exit(EXIT_SUCCESS);
	}
	//2.创建新会话
	//创建新会话后,当前进程就跟原会话和控制终端断开连接,不再受其影响
	if(setsid() == -1) {
		perror("setsid error");
		return -1;
	}
	//3.创建子进程,
	//因为setsid后,进程成了新会话和新进程组的首进程,即领导进程,还是能够打开控制终端;为了防止进程重新获取控制终端,再次fork
	pid = fork();
	if(pid == -1) {
		perror("fork error");
		return -1;
	}
	if(pid > 0) {
		_exit(EXIT_SUCCESS);
	}
	signal(SIGCHLD, SIG_IGN);
	//4.设置掩码
	umask(0);
	//5.切换工作目录至根目录
	//chdir("/");
	//6.重定向标准输入输出
	closefd();
	dup2file(logfile);	
}

int main(int argc, char* argv[]) {
	char logfile[MAXPATHLEN] = "";
	getcwd(logfile, MAXPATHLEN);
	strncat(logfile, "/nohup.log", 10);

	if (argc < 2) {
		printf("Usage: %s [command]\n", argv[0]);
		return -1;
	}

	mydaemon(logfile);
	if (execlp(argv[1], argv[1], NULL) == -1) {
		perror("exec error");
	}
	return 0;
}

 

你可能感兴趣的:(linux,C)