How to run a node from swift

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



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

Replies

I am trying to stop a nodejs server started using forever

What platform are you working on?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I am working on Mojave MacOS.

I am working on Mojave MacOS.

OK.

There are a lot of connected parts here, and I recommend that you try teasing them apart to see whether things start going wrong:

  1. First try running a simple command-line tool using

    Process
    . My preferred tool for this sort of thing is
    /bin/echo
    .
  2. Next, try extending that to Node itself.

  3. Finally, rope in

    forever
    .

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I tries /bin/ls. It is printing list of files but when i run forever it throws error

What about that intermediate step, running Node without

forever
?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Sorry, i missed intermediate step. running node throws me below error


sh: /usr/local/bin/node: Operation not permitted

running node throws me below error

OK, that’s not good.

OK, next question: Is your app sandboxed?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

My App is not sandboxed !!

My App is not sandboxed !!

Hmmmm, that rules out the most common cause of

EPERM
errors (which is what
Operation not permitted
translates to). Beyond that, I’ve no good idea as to what’s triggering this error.

It looks like this error is being reported by

sh
, indicating that it can’t invoke
/usr/local/bin/node
. Are you sure that
/usr/local/bin/node
is the right path? And that there’s a runnable copy of
node
there?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yes i have it there

ls -la /usr/local/bin/node

-rwxr-xr-x 1 root wheel 41796640 Sep 4 21:55 /usr/local/bin/node



Is it some access related issue caused in mojave due to full disc access or something ?

Is it some access related issue caused in mojave due to full disc access or something ?

/usr/local/
does not have any special protection in that regard. You might run into problem if your app is sandboxed, but you assure me that’s not the case.

Just for grins I made a copy of

/bin/echo
, signed it myself, and installed it as
/usr/local/bin/qecho
. I was then able to run it like so:
NSLog("will launch")
let p = Process()
p.launchPath = "/usr/local/bin/qecho"
p.arguments = ["Hello", "Cruel", "World!"]
p.terminationHandler = { _ in
    NSLog("did terminate")
}
p.launch()
NSLog("did launch")

This is on 10.14.6.

It looks like you’re running

node
via
sh
. What happens if you run it directly?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks eskimo can you please point to me some reference to understand which locations are restricted to access in Mojave and catalina. I want to learn how Full disk access works.

I want to learn how Full disk access works.

I’m skeptical that this has anything to do with Full Disk Access but…

On the 10.14 side of things, the best info is that in WWDC 2018 Session 702 Your Apps and the Future of macOS Security. The changes in 10.15 beta are covered by WWDC 2019 Session 701 Advances in macOS Security.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"