title: Background Process (Daemon) in C not execvp() -ing
body: <p>So, I am trying to run a background process and execvp from it.
  When i enter cp /path/file /var/tmp, the process is not copying the file.</p>
  
  <p>Here is my code for reference:</p>
  
  <pre><code>void cmd_bg(char command[])
  {
          pid_t process_id = 0;
          pid_t sid = 0;
          char* argv[512];
          getArgv(command,argv);
          // Create child process
          process_id = fork();
          // Indication of fork() failure
          if (process_id &lt; 0)
          {
                  printf("fork failed!\n");
                  // Return failure in exit status
                  exit(1);
          }
          // PARENT PROCESS. Need to kill it.
          if (process_id &gt; 0)
          {
                  printf("process_id of child process %d \n", process_id);
                  // return success in exit status
                  exit(0);
          }
          //unmask the file mode
          umask(0);
          //set new session
          sid = setsid();
          if(sid &lt; 0)
          {
                  // Return failure
                  exit(1);
          }
          // Change the current working directory to root.
          chdir("/");
          // Close stdin. stdout and stderr
          close(STDIN_FILENO);
          close(STDOUT_FILENO);
          close(STDERR_FILENO);
  
  
          execvp(argv[0],argv);
  
          }
  }
  void getArgv(char command[], char* argv[512])
  {
          char *token;
          int count = 0;
          int pid = 0;
          int ex = 0;
          char *absPath;
          char pwdtemp[512];
          strcpy(pwdtemp,pwd);
  
          token = strtok(command, " ");
          while(token!=NULL)
          {
                  argv[count++] = token;
                  printf("%s\n",argv[count-1]);
                  token = strtok(NULL," ");
          }
          argv[count] = '\0';
  }
  </code></pre>
  
  <p>I sincerely hope someone can help me. Thanks!</p>
  
  <p><strong>EDIT</strong>: I FOUND THE SOLUTION. I CANT SELF-ANSWER BECAUSE I DONT HAVE 100 REPS YET.
  ANYWAYS FOR PEOPLE WHO MIGHT VIEW THIS THREAD IN THE FUTURE:</p>
  
  <p>OK. I figured out the problem. </p>
  
  <p>Firstly I Commented out the </p>
  
  <pre><code>close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  chdir("/");
  </code></pre>
  
  <p>Next, instead of calling the cmd_bg directly, I created:</p>
  
  <pre><code>void temp1(char command[])
  {
      int pid = fork();
      if(pid==0)
          cmd_bg(command);
  
      else
          waitpid(-1, NULL, 0);
  }
  </code></pre>
  
  <p>It seems to work now! Thanks a tonne for your input guys!</p>