From: Claudio Cambra Date: Thu, 19 Sep 2024 12:10:46 +0000 (+0800) Subject: Correctly kill processes spawned by mac-crafter if mac-crafter quits/is killed/etc X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~12^2~6^2~14^2 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=b6def1176f634ead9b9268712a8f3acafe6d4f9f;p=nextcloud-desktop.git Correctly kill processes spawned by mac-crafter if mac-crafter quits/is killed/etc Signed-off-by: Claudio Cambra --- diff --git a/admin/osx/mac-crafter/Sources/Utils/Shell.swift b/admin/osx/mac-crafter/Sources/Utils/Shell.swift index a240ccffa..1d7a35522 100644 --- a/admin/osx/mac-crafter/Sources/Utils/Shell.swift +++ b/admin/osx/mac-crafter/Sources/Utils/Shell.swift @@ -14,6 +14,8 @@ import Foundation +var task: Process? + @discardableResult func run( _ launchPath: String, @@ -21,24 +23,31 @@ func run( env: [String: String]? = nil, quiet: Bool = false ) -> Int32 { - let task = Process() - task.launchPath = launchPath - task.arguments = args + defer { task = nil } + task = Process() + + signal(SIGINT) { _ in + task?.terminate() // Send terminate signal to the task + exit(0) // Exit the script after cleanup + } + + task?.launchPath = launchPath + task?.arguments = args if let env, - let combinedEnv = task.environment?.merging(env, uniquingKeysWith: { (_, new) in new }) + let combinedEnv = task?.environment?.merging(env, uniquingKeysWith: { (_, new) in new }) { - task.environment = combinedEnv + task?.environment = combinedEnv } if quiet { - task.standardOutput = nil - task.standardError = nil + task?.standardOutput = nil + task?.standardError = nil } - task.launch() - task.waitUntilExit() - return task.terminationStatus + task?.launch() + task?.waitUntilExit() + return task?.terminationStatus ?? 1 } func run(