Skip to content

Instantly share code, notes, and snippets.

@lintianzhi
Created December 8, 2012 10:04
Show Gist options
  • Save lintianzhi/4239671 to your computer and use it in GitHub Desktop.
Save lintianzhi/4239671 to your computer and use it in GitHub Desktop.
daemonize the program
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
int main(int argc, char *argv[])
{
int i;
pid_t pid;
if(argc < 2)
{
printf("At least 2 arguments\n");
return 0;
}
pid = fork();
if(pid == 0)
{
// freopen("/dev/null","w",stdout);
setsid();
signal(SIGHUP,SIG_IGN);
i = execv(argv[1], &argv[1]);
if(i < 0)
printf("Error of: %s\n",argv[1]);
return 0;
}
printf("daemon pid: %d\n", pid);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment