I've a very simple MacOS Swift application which is used as a launcher to provide a dock icon. The application launches the main script. But if the application is terminated (e.g. by clicking on it and selecting "Close" in the context menu) the subprocess with the script continues to run.
How to change this behaviour to kill the child process if the application is killed. Using pgrep I checked and the script is executed in a subprocess:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(_ aNotification: Notification) {
print("App started!\n");
DispatchQueue.global(qos: .background).async {
let process = Process()
process.executableURL = URL(fileURLWithPath:"/usr/local/bin/node")
process.arguments = ["./build/index.js"]
process.currentDirectoryPath = "/Users/myaccount/basedir"
process.terminationHandler = { (process) in
print("didFinish")
}
do {
try process.run()
} catch {}
}
}
func applicationWillTerminate(_ aNotification: Notification) {
print("I'll never show up\n")
}
}
