Joshua Soileau
Joshua Soileau

Reputation: 3025

Fork() and Execl() in C++, Execl() is failing

I am trying to run execl() in C++, but it fails and I'm not sure why.

  string fileName = "test.txt";
  string computerName = "[email protected]";
  pid_t pid;
  int    status;

  if ((pid = fork()) < 0) {     /* fork a child process           */
     printf("*** ERROR: forking child process failed\n");
     exit(1);
  }
  else if (pid == 0) {          /* for the child process:         */
     if (execl("scp", fileName.c_str(), computerName.c_str(), NULL) < 0) {     /* execute the command  */
        printf("*** ERROR: exec failed\n");
        exit(1);
     }
  }
  else {                                /* for the parent:      */
     while (wait(&status) != pid)       /* wait for completion  */
     ;
  }

When I run this, my execl() call fails and I print out

*** ERROR: exec failed

Upvotes: 1

Views: 3131

Answers (2)

unxnut
unxnut

Reputation: 8839

Your execl syntax is not correct. You need to specify the complete path to scp (including scp) as the first argument, followed by your list of arguments.

Upvotes: 0

Robᵩ
Robᵩ

Reputation: 168836

You are using the exec variant that requires a full path as the first argument.

Replace:

execl( "scp", ... )

with either

execl("/usr/bin/scp", ...)

or

execlp("scp", ... )

Reference: http://linux.die.net/man/3/execl

Upvotes: 5

Related Questions