#include #include #include #include #include char debug_mode = 0; int main(int argc, char ** argv){ if(argc > 1 && !strcmp(argv[1], "-d")) debug_mode = 1; if(!debug_mode){ // Become a daemon pid_t pid = fork(); if(pid) { // We're the parent, because pid is the pid of the child return 0; } setsid(); } int log_fd = open("daemon_log", O_CREAT | O_TRUNC | O_WRONLY, 0644); if(log_fd == -1){ perror("daemon_log"); return 1; } else if(debug_mode) printf("Logging to daemon_log\n"); dup2(log_fd, 1); dup2(log_fd, 2); printf("Starting daemon\n"); for(;;){ printf("beep\n"); sleep(5); } return 0; }