目录

类

class CurrentProcess

public class CurrentProcess <: Process

功能:此类为当前进程类,继承 Process 类,提供对当前进程操作相关功能。

提供功能具体如下:

  • 提供获取当前进程标准流(stdIn、stdOut、stdErr)机制。
  • 提供当前进程退出注册回调函数机制。
  • 提供当前进程退出机制,允许设置退出状态码。

[h2]prop stdErr

public prop stdErr: OutputStream

功能:获取当前进程标准错误流。

类型:OutputStream

[h2]prop stdIn

public prop stdIn: InputStream

功能:获取当前进程标准输入流。

类型:InputStream

[h2]prop stdOut

public prop stdOut: OutputStream

功能:获取当前进程标准输出流。

类型:OutputStream

[h2]func atExit(() -> Unit)

public func atExit(callback: () -> Unit): Unit

功能:注册回调函数,当前进程退出时执行注册函数。

请不要使用C语言 atexit 函数,避免出现不可期问题。

参数:

[h2]func exit(Int64)

public func exit(code: Int64): Nothing

功能:进程退出函数,执行到此函数直接结束当前进程,并且通过入参 code 设置返回状态码。

参数:

class Process

public open class Process

功能:此类为进程类,提供进程操作相关功能。

提供功能具体如下:

  • 提供获取当前进程实例的功能。
  • 提供根据进程 id 绑定进程实例的功能。
  • 提供根据输入信息创建子进程的功能。
  • 提供获取进程信息的功能。
  • 提供关闭进程的功能,允许设置是否强制关闭进程。

[h2]prop arguments

public prop arguments: Array<String>

功能:获取进程参数。Windows 平台下无法在非特权 API 下获取到本属性,暂不支持获取。

类型:Array<String>

异常:

[h2]prop command

public prop command: String

功能:获取进程命令。

类型:String

异常:

[h2]prop commandLine

public prop commandLine: Array<String>

功能:获取进程命令行。Windows 平台当前进程可获取,其他场景下无法在非特权 API 下获取到本属性,暂不支持获取。

类型:Array<String>

异常:

[h2]prop current

public static prop current: CurrentProcess

功能:获取当前进程实例。

类型:CurrentProcess

[h2]prop environment

public prop environment: Map<String, String>

功能:获取进程环境变量。Windows 平台当前进程可获取,其他场景下无法在非特权 API 下获取到本属性,暂不支持获取。

类型:Map<String, String>

异常:

[h2]prop name

public prop name: String

功能:获取进程名。

类型:String

异常:

[h2]prop pid

public prop pid: Int64

功能:获取进程 id。

类型:Int64

[h2]prop workingDirectory

public prop workingDirectory: Path

功能:获取进程工作路径。Windows 平台当前进程可获取,其他场景下无法在非特权 API 下获取到本属性,暂不支持获取。

类型:Path

异常:

[h2]static func of(Int64)

public static func of(pid: Int64): Process

功能:根据输入进程 id 绑定一个进程实例。

参数:

返回值:

异常:

[h2]static func run(String, Array<String>, Path, Map<String, String>, ProcessRedirect, ProcessRedirect,ProcessRedirect, Duration)

public static func run(command: String,
                      arguments: Array<String>,
                      workingDirectory!: ?Path = None,
                      environment!: ?Map<String, String> = None,
                      stdIn!: ProcessRedirect = Inherit,
                      stdOut!: ProcessRedirect = Inherit,
                      stdErr!: ProcessRedirect = Inherit,
                      timeout!: ?Duration = None): Int64

功能:根据输入参数创建并运行一个子进程,等待该子进程运行完毕并返回子进程退出状态。

  • 在 Windows 平台上,在子进程执行完成后立即删除子进程的可执行文件可能删除失败并抛出异常,异常信息为 Access is denied,如果遇到该问题,可以在一小段延迟后重新尝试删除该文件,详细实现可参考示例。

参数:

返回值:

异常:

[h2]static func runOutput(String, Array<String>, Path, Map<String, String>, ProcessRedirect, ProcessRedirect, ProcessRedirect)

public static func runOutput(command: String,
                            arguments: Array<String>,
                            workingDirectory!: ?Path = None,
                            environment!: ?Map<String, String> = None,
                            stdIn!: ProcessRedirect = Inherit,
                            stdOut!: ProcessRedirect = Pipe,
                            stdErr!: ProcessRedirect = Pipe): (Int64, Array<Byte>, Array<Byte>)

功能:根据输入参数创建并运行一个子进程,等待该子进程运行完毕并返回子进程退出状态、标准输出和标准错误。输出流、错误流中包含大量输出的场景不适用于本函数,建议通过 SubProcess 中提供的标准流属性结合 wait 函数自行处理。

参数:

返回值:

异常:

[h2]static func start(String, Array<String>, Path, Map<String, String>, ProcessRedirect, ProcessRedirect, ProcessRedirect)

public static func start(command: String,
                        arguments: Array<String>,
                        workingDirectory!: ?Path = None,
                        environment!: ?Map<String, String> = None,
                        stdIn!: ProcessRedirect = Inherit,
                        stdOut!: ProcessRedirect = Inherit,
                        stdErr!: ProcessRedirect = Inherit): SubProcess

功能:根据输入参数创建并运行一个子进程,并返回一个子进程实例。调用该函数创建子进程后,需要调用 wait 或 waitOutput 函数,否则该子进程结束后成为的僵尸进程的资源不会被回收。

参数:

返回值:

异常:

[h2]func terminate(Bool)

public func terminate(force!: Bool = false): Unit

功能:终止进程。

参数:

返回值:

异常:

class SubProcess

public class SubProcess <: Process

功能:此类为子进程类,继承 Process 类,提供对子进程操作相关功能。

提供功能具体如下:

  • 提供获取子进程标准流(stdIn、stdOut、stdErr)机制。
  • 提供等待子进程执行返回退出状态码机制,允许设置等待超时时长。
  • 提供等待子进程执行返回输出结果(包含运行正常、异常结果)机制,允许设置等待超时时长。

[h2]prop stdErr

public prop stdErr: InputStream

功能:获取输入流,连接到子进程标准错误流。

类型:InputStream

[h2]prop stdIn

public prop stdIn: OutputStream

功能:获取输出流,连接到子进程标准输入流。

类型:OutputStream

[h2]prop stdOut

public prop stdOut: InputStream

功能:获取输入流,连接到子进程标准输出流。

类型:InputStream

[h2]func wait(Duration)

public func wait(timeout!: ?Duration = None): Int64

功能:阻塞当前进程等待子进程任务执行完成并返回子进程退出状态码,允许指定等待超时时间。对于需要操作标准流的场景(Pipe 模式),使用者需要优先处理标准流,避免子进程标准流缓冲区满后调用本函数产生死锁。

超时时间处理机制:

  • 未传参、 timeout 值为 None 或值小于等于 Duration.Zero 时,阻塞等待直至子进程执行返回。
  • timeout 值大于 Duration.Zero 时,阻塞等待子进程执行返回或等待超时后抛出超时异常。

参数:

返回值:

异常:

[h2]func waitOutput()

public func waitOutput(): (Int64, Array<Byte>, Array<Byte>)

功能:阻塞当前进程等待子进程任务执行完成,并返回子进程退出状态码、返回结果(包含输出流和错误流返回结果)。输出流、错误流中包含大量输出的场景不适用于本函数,建议通过 SubProcess 中提供的标准流属性结合 wait 函数自行处理。

返回值:

异常: