Hi!
I wrote a backup console application. It does use ssh/sshfs to access a web server via public key. Also it does send an SMTP-mail when finished. If the process is started via
launchctl load DAEMON
it works always properly (RunAtLoad is true). But when the daemon is called automatically at night it does not always work. In my log I get these errors:
ssh: connect to host SERVERURL port 22: Undefined error: 0
and
Failed sending: NIOConnectionError(host: "mail.mydomain", port: 25, dnsAError: Optional(NIOCore.SocketAddressError.unknown(host: "mail.mydomain", port: 25)), dnsAAAAError: Optional(NIOCore.SocketAddressError.unknown(host: "mail.mydomain", port: 25)), connectionErrors: [])
it does run on a MacMini M1 with macOS 12.2.
Any clue what's wrong or how to find the reason of this issue?
PS. On another MacMini (Intel) with macOS 11.6 the backup works since a year but there is always an admin user logged in.
Now I got it working. I use a check at start of my application with following code:
if !NetworkCheck().waitForNetwork() {
print("Error: No network available!")
exit(exitCodes.NoNetwork.rawValue)
}
and this is my NetworkCheck class:
import Foundation
import Network
class NetworkCheck {
private let networkMonitor : NWPathMonitor
init() {
self.networkMonitor = NWPathMonitor(requiredInterfaceType: .wiredEthernet)
self.networkMonitor.start(queue: .global())
}
func waitForNetwork(networkTimeout : Double = 30.0) -> Bool {
let networkLookupStart = Date.now
var isConected = false
while abs(networkLookupStart.timeIntervalSinceNow) <= networkTimeout {
networkMonitor.pathUpdateHandler = { path in
if path.status == .satisfied {
isConected = true
}
}
if isConected {
break
}
sleep(1)
}
return isConected
}
deinit {
networkMonitor.cancel()
}
}