在Node.js中,如何确定子进程是否已启动且没有错误?

时间:2022-02-26 07:32:43
var spawn = require('child_process').spawn;
var child = spawn('some-command');

I know I can guard against ENOENT (when some-command doesn't exist) with

我知道我可以防范ENOENT(当某些命令不存在时)

child.on('error', function(err) { ... })

Now, how do I asynchronously determine the process is running and no error has happened?

现在,我如何异步确定进程正在运行并且没有发生错误?


I could listen for error and close events, but that still leaves the case of "is running" looking identical to "the operating system hasn't gotten around to looking for the file yet", which can cause nasty race conditions.

我可以听错误和关闭事件,但仍然留下“正在运行”的情况看起来与“操作系统尚未找到文件”相同,这可能会导致令人讨厌的竞争条件。

An open-event would be nice, but the docs don't mention one.

一个开放的活动会很好,但文档没有提到一个。

Does a functional workaround exist?

是否存在功能性解决方案?

1 个解决方案

#1


2  

The information you are looking for (process running, no errors) is not available from the Host OS in such an easy-to-use format.

Unless the child process prints something that is parsed and tracked (by code you or someone will have to write) in node, or exits with a status code, there is no indication available from the OS that nodejs or iojs can obtain from a system call and wrap in a JS API for the developer.

除非子进程在节点中打印出被解析和跟踪的内容(通过代码您或某人必须编写),或者使用状态代码退出,否则操作系统无法获得nodejs或iojs可以从系统调用中获取的内容并为开发人员提供JS API。

At least on Linux, the operating system status of a process is limited to one of:

至少在Linux上,进程的操作系统状态仅限于以下之一:

  1. process is running; OR
  2. 进程正在运行;要么

  3. process has exit (status number indicates OK or ERROR); OR
  4. 进程有退出(状态号表示OK或ERROR);要么

  5. non-existent (no such PID)
  6. 不存在(没有这样的PID)

Furthermore, once the exit status has been retrieved with wait() or waitpid(), it is no longer available.

此外,一旦使用wait()或waitpid()检索退出状态,它就不再可用。

The idea of an "error" is often application dependent and these application errors are not tracked by the operating system -- except for the exit status integer the process reports when it exits.

“错误”的概念通常取决于应用程序,并且操作系统不会跟踪这些应用程序错误 - 除了进程在退出时报告的退出状态整数。

To give a clearer example, many apps have commands that open files for processing, and will print an error message when an input file can not be opened and proceed to the next command. This failure is not part of a process status that is tracked by PID in the operating system and kept in memory somewhere so it can be read from another process. It may appear in the stderr or stdout stream and can be read that way, but requires specific coding for it to be interpreted correctly by parent or other processes. Alternatively, other apps will exit immediately when something has gone seriously wrong, and set exit status to a non-zero number, indicating error. That exit status and the fact that the process terminated are available from the operating system.

为了给出更清楚的示例,许多应用程序具有打开文件进行处理的命令,并且在无法打开输入文件时将打印错误消息并继续执行下一个命令。此故障不是由操作系统中的PID跟踪并在某处保存在内存中的进程状态的一部分,因此可以从另一个进程读取该故障。它可能出现在stderr或stdout流中,并且可以以这种方式读取,但需要特定的编码才能由父进程或其他进程正确解释。或者,当出现严重错误时,其他应用程序将立即退出,并将退出状态设置为非零数字,表示错误。退出状态和流程终止的事实可从操作系统获得。

See also: Just check status process in c

另请参阅:只需检查c中的状态过程

#1


2  

The information you are looking for (process running, no errors) is not available from the Host OS in such an easy-to-use format.

Unless the child process prints something that is parsed and tracked (by code you or someone will have to write) in node, or exits with a status code, there is no indication available from the OS that nodejs or iojs can obtain from a system call and wrap in a JS API for the developer.

除非子进程在节点中打印出被解析和跟踪的内容(通过代码您或某人必须编写),或者使用状态代码退出,否则操作系统无法获得nodejs或iojs可以从系统调用中获取的内容并为开发人员提供JS API。

At least on Linux, the operating system status of a process is limited to one of:

至少在Linux上,进程的操作系统状态仅限于以下之一:

  1. process is running; OR
  2. 进程正在运行;要么

  3. process has exit (status number indicates OK or ERROR); OR
  4. 进程有退出(状态号表示OK或ERROR);要么

  5. non-existent (no such PID)
  6. 不存在(没有这样的PID)

Furthermore, once the exit status has been retrieved with wait() or waitpid(), it is no longer available.

此外,一旦使用wait()或waitpid()检索退出状态,它就不再可用。

The idea of an "error" is often application dependent and these application errors are not tracked by the operating system -- except for the exit status integer the process reports when it exits.

“错误”的概念通常取决于应用程序,并且操作系统不会跟踪这些应用程序错误 - 除了进程在退出时报告的退出状态整数。

To give a clearer example, many apps have commands that open files for processing, and will print an error message when an input file can not be opened and proceed to the next command. This failure is not part of a process status that is tracked by PID in the operating system and kept in memory somewhere so it can be read from another process. It may appear in the stderr or stdout stream and can be read that way, but requires specific coding for it to be interpreted correctly by parent or other processes. Alternatively, other apps will exit immediately when something has gone seriously wrong, and set exit status to a non-zero number, indicating error. That exit status and the fact that the process terminated are available from the operating system.

为了给出更清楚的示例,许多应用程序具有打开文件进行处理的命令,并且在无法打开输入文件时将打印错误消息并继续执行下一个命令。此故障不是由操作系统中的PID跟踪并在某处保存在内存中的进程状态的一部分,因此可以从另一个进程读取该故障。它可能出现在stderr或stdout流中,并且可以以这种方式读取,但需要特定的编码才能由父进程或其他进程正确解释。或者,当出现严重错误时,其他应用程序将立即退出,并将退出状态设置为非零数字,表示错误。退出状态和流程终止的事实可从操作系统获得。

See also: Just check status process in c

另请参阅:只需检查c中的状态过程