Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. // Simplistic daemonize tool by /u/Craftkorb
  2. // $ gcc -o d daemonize.c
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6.  
  7. int main (int argc, char **argv) {
  8.    
  9.     if (argc < 2) {
  10.         printf ("Usage: %s <Command ...>\n", argv[0]);
  11.         return 1;
  12.     }
  13.    
  14.     //
  15.     if (fork () == 0) {
  16.        
  17.         close (fileno(stdout));
  18.         close (fileno(stderr));
  19.         close (fileno(stdin));
  20.        
  21.         open ("/dev/null", O_WRONLY | O_NONBLOCK);
  22.         open ("/dev/null", O_RDONLY | O_NONBLOCK);
  23.         open ("/dev/null", O_RDONLY | O_NONBLOCK);
  24.        
  25.         execvp (argv[1], argv + 1);
  26.        
  27.         perror ("Failed to execute");
  28.         return errno;
  29.        
  30.     }
  31.    
  32.     //
  33.     return 0;
  34.    
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement