【问题标题】:How to run a node from swift如何从 swift 运行节点
【发布时间】:2019-12-20 17:48:42
【问题描述】:

我正在尝试停止使用以下命令永久使用的nodejs 服务器:

func shell(command: String) -> Int32 {
        let task = Process()
        task.launchPath = "/usr/bin/env"
        task.arguments = ["sh", "-c", command]
        task.launch()
        task.waitUntilExit()
        return task.terminationStatus
    }

shell(command: "$forever_PATH/forever stop node.js")

我看到以下错误:

env: node: No such file or directory
PS:node is located in /usr/local/bin

【问题讨论】:

  • 似乎是环境变量未设置,您可以通过给出节点的硬代码路径来尝试
  • launchPath 在 10.13 中已弃用。尝试使用task.currentDirectoryURLtask.executableURL。欲了解更多信息developer.apple.com/documentation/foundation/process

标签: swift macos forever


【解决方案1】:
  func shell(path:String,commandargs: [String]) -> Bool
    {
        let task = Process()
        var ret : Bool = false

      task.launchPath = path
      task.arguments = commandargs
      task.launch()
      task.waitUntilExit()
        if !task.isRunning {
            let status = task.terminationStatus
            if status == 0 {
                ret = true
            } else {
                ret = false
            }
        }
         return ret

    }

如下调用它

shell(path: "/usr/local/bin/node", commandargs: ["PathToforever","stop","nodejsscript.js"])

【讨论】:

    【解决方案2】:

    我认为这可能是一个沙盒问题。进入您应用的权利页面并删除沙盒部分。

    这解决了我找不到文件的问题。

    请注意,您将无法将应用发布到 App Store。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多