简体   繁体   English

通过系统调用启动bash脚本的Linux C ++程序始终返回-1

[英]Linux C++ program launching a bash script via system call always returns -1

I have a chain of programs as follows: 我有一系列程序如下:

C++ program "A" launches another C++ program "B" which launches a bash script. C ++程序“A”启动另一个C ++程序“B”,它启动一个bash脚本。

To launch the bash script, I am using: 要启动bash脚本,我正在使用:

int returnVal = system("pathToScript/myScript.sh");

I can see the output of the script in my log file, so it's definitely being executed. 我可以在我的日志文件中看到脚本的输出,所以它肯定正在执行。

Problem is, returnVal is always -1 no matter what the script returns. 问题是,无论脚本返回什么,returnVal总是-1。 I even hard coded an "exit 3" in the script and I'm still getting a returnVal of -1 when I launch it via the system call. 我甚至在脚本中硬编码了“exit 3”,当我通过系统调用启动它时,我仍然得到一个-1的returnVal。

Running the script standalone in a terminal and echoing "$?" 在终端中独立运行脚本并回显“$?” shows a return value of 3 as expected. 按预期显示返回值3。

So why is the exit code breaking when I run it through a chain of C++ programs? 那么,当我通过一系列C ++程序运行它时,为什么退出代码会破坏? Is there any way to get around this? 有没有办法解决这个问题?

EDIT - Using perror shows a "No child processes" error message. 编辑 - 使用perror显示“无子进程”错误消息。

EDIT - As an alternative, I'm trying to use fork/exec/wait to execute my script, but I'm getting random exit codes such as 182, 56, 163, 62, 51, etc... Code below: 编辑 - 作为替代方案,我正在尝试使用fork / exec / wait来执行我的脚本,但我得到随机退出代码,如182,56,163,62,51等......代码如下:

pid_t pid = vfork();

switch (pid)
{
  case -1:
     cout << "Failed to fork." << endl;

  case 0: // Child process
     cout << "Child process launched!" << endl;
     execl("/pathToScript/myScript.sh", "/pathToScript/myScript.sh", "someArgument", NULL);
     cout << "execl call failed." << endl;
     exit(0);

  default:
     int status;

     cout << "Waiting for process to complete..." << endl;

     waitpid(pid, &status, 0); // Wait for the process to complete.

     cout << "Process exited with status: " << WEXITSTATUS(status) << endl;
}

Why am I getting random exit status here? 为什么我在这里获得随机退出状态?

Appreciate any advice. 感谢任何建议。 Thanks! 谢谢!

As mentioned in comments your problem is ignorig SIGCHLD . 正如评论中所提到的,您的问题是无视SIGCHLD Ignoring SIGCHLD can be used to prevent the creation of zombies. 忽略SIGCHLD可用于防止僵尸的创建。 So by setting it disposition to default you can break something. 因此,通过将其处置设置为默认值, 您可以破坏某些内容。

Here example: 这里的例子:

signal (SIGCHLD, SIG_DFL);
system ("some_program");
signal (SIGCHLD, SIG_IGN);

If some child process (not child in system ) terminated between execution first and third line, it will become a zombie. 如果某个子进程(不是system子进程)在执行第一行和第三行之间终止,它将变成一个僵尸。

If you want solve this resources leak correctly, you should use another method (double fork or wait ) for solving zombies problem. 如果你想正确解决这个资源泄漏,你应该使用另一种方法(双forkwait )来解决僵尸问题。


Why am I getting random exit status here? 为什么我在这里获得随机退出状态?

Because waitpid call failed. 因为waitpid调用失败了。 If you print out return value of waitpid it will be -1 . 如果打印出waitpid返回值,它将为-1 The value of errno most likely will be ECHILD . errno的值很可能是ECHILD Setting disposition of SIGCHLD to SIG_IGN causes breaking wait and waitpid . SIGCHLD设置为SIG_IGN会导致中断waitwaitpid In your example waitpid writes nothing to status variable (or writes trash). 在您的示例中, waitpid不会向status变量写入任何内容(或写入垃圾箱)。

182, 56, 163, 62, 51, etc.. 182,56,163,62,51等。

This numbers are values of uninitialized integer variable (or trash). 此数字是未初始化的整数变量(或垃圾)的值。

As an alternative, I'm trying to use fork/exec/wait to execute my script 作为替代方案,我正在尝试使用fork / exec / wait来执行我的脚本

It is not good alternative. 这不是一个好的选择。 Alternative solution is set disposition of SIGCHLD to SIG_DFL for all parts of program. 替代解决方案是为程序的所有部分设置SIGCHLDSIG_DFL配置。 And change strategy of handling zombies in other parts. 并改变其他部分处理僵尸的策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM