我试图在C++中运行execl(),但是失败了,我不知道为什么。
string fileName = "test.txt";
string computerName = "jssoile@palmetto.school.edu";
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 */
;
}当我运行这段代码时,execl()调用失败并输出
*** ERROR: exec failed发布于 2013-04-02 03:17:43
您正在使用需要完整路径作为第一个参数的exec变体。
替换:
execl( "scp", ... )无论使用哪一种
execl("/usr/bin/scp", ...)或
execlp("scp", ... )参考:http://linux.die.net/man/3/execl
发布于 2013-04-02 03:18:13
您的execl语法不正确。您需要指定scp (包括scp)的完整路径作为第一个参数,后跟参数列表。
https://stackoverflow.com/questions/15750068
复制相似问题