Search code examples
swiftmacosforever

How to run a node from swift


I am trying to stop a nodejs server started using forever using below command:

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")

I am seeing below error:

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

Solution

  •   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
    
        }
    

    Call it as below

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