Simple NSTask wrapper to run shell command synchronously or asynchronously
int status;
NSData* result = runCommandSync(@"/usr/bin/curl -fsSL taobao.com", YES, &status);
NSLog(@"data length: %ld, status: %d", [result length], status);
runCommandAsync(@"/usr/bin/curl -fsSL taobao.com", YES, ^(NSData * _Nonnull data, int exitStatus) {
NSLog(@"data length: %ld, status: %d", [data length], exitStatus);
});
runCommandAsyncTimeout(@"echo begin;sleep 10;echo end", YES, 2, ^(NSData * _Nonnull data, int exitStatus) {
if (exitStatus == CMD_TIMEOUT_ERR) {
NSLog(@"command running timeout: %@", data);
} else {
NSLog(@"command complete: %@", data);
}
});