Results 1 to 4 of 4

Thread: Help with execvp function in C program !

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Help with execvp function in C program !

    I knew that execvp exec a child process with its argument list.
    Code:
    int main ()
    {
      /* The argument list to pass to the “ls” command. */
      char* arg_list[] = {
         “ls”,     /* argv[0], the name of the program. */
         “-l”,
         “/”,
         NULL      /* The argument list must end with a NULL.  */
      };
    pid_t child_pid;
    /* Duplicate this process. */
    child_pid = fork ();
    if (child_pid != 0)
      /* This is the parent process. */
      return child_pid;
    else {
      /* Now execute PROGRAM, searching for it in the path. */
      execvp (ls, arg_list);
    
      fprintf (stderr, “an error occurred in execvp\n”);
      abort ();
    
      printf (“done with main program\n”);
      return 0;
    }
    But if I want to execute my shell or php script in exec
    Code:
    phpPATH=/usr/local/bin/php
    scriptPATH=/usr/local/apache/htdocs/myweb/myscript.php
    I've failed with
    Code:
    char* arg_list[] = {
         “phpPATH”,
         “scriptPATH”,
         “scriptArgument”,
         NULL      /* The argument list must end with a NULL.  */
      };
    ----------------
    execvp(phpPATH,arg_list)
    How can I use execvp with my own script ?
    Thanks in advanced !

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,500
    Thanks
    17
    Thanked 789 Times in 507 Posts
    Rep Power
    10

    Default

    Is it Linux or any other UNIX box? Also, do you get any specific error code or message? For further debugging try
    PHP Code:
    strace ./a.out
    strace 
    ./yourapp 
    Last edited by nixcraft; 17th June 2009 at 12:08 AM.
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  3. #3
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,500
    Thanks
    17
    Thanked 789 Times in 507 Posts
    Rep Power
    10

    Default

    Here is my test.c
    PHP Code:
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
       
    pid_t pid;
       
    char *const parmList[] = 
       {
    "/usr/bin/php""/tmp/test.php"NULL};

       if ((
    pid fork()) == -1)
          
    perror("fork() error");
       else if (
    pid == 0) {
          
    execvp("/usr/bin/php"parmList);
          
    printf("Return not expected. Must be an execvp() error.\n");
       }

    Here is my test.php
    PHP Code:
    #!/usr/bin/php
    <?php
        
    echo "Test\n";
    ?>
    Output:
    Code:
    ./test
    Test
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  4. #4
    Junior Member
    Join Date
    Apr 2008
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    thank you so much. IT's working !

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Adding storage function to a mail server
    By satimis in forum Databases servers
    Replies: 1
    Last Post: 17th April 2012, 02:01 AM
  2. fattach function which package
    By sathiya in forum Linux software
    Replies: 2
    Last Post: 2nd June 2008, 05:41 PM
  3. is any one good in c program ?
    By oronno in forum Shell scripting
    Replies: 0
    Last Post: 19th September 2007, 01:04 PM
  4. start program
    By schaapmansz in forum Getting started tutorials
    Replies: 1
    Last Post: 26th June 2007, 09:49 PM
  5. call function in bash
    By mala_un in forum Shell scripting
    Replies: 9
    Last Post: 18th August 2006, 01:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •