I need to know when a command line program is stopped by the user to release some active bluetooth connections from a command-line program (running on the terminal), written in swift.
Say the user calls the program then exits by pressing ctrl+Z.
How would I know ?
You can install a signal handler with Swift. For example:
import Foundation
let startTime = Date()
var signalReceived: sig_atomic_t = 0
signal(SIGINT) { signal in signalReceived = 1 }
var i = 0
while true {
if signalReceived == 1 { break }
usleep(500_000)
if signalReceived == 1 { break }
i += 1
print(i)
}
let endTime = Date()
print("Program has run for \(endTime.timeIntervalSince(startTime)) seconds")
Modified from this gist.