How to catch error when setting launchpath in NSTask
unfortunately, there is no chance to catch runtime exceptions. with try / catch you can recovery from trowing error, not from runtime exception. you can create you own exception handler, but you are still not able to recover from it. try to lunch from NSTask some common shell with command as a parameter and next use a pipe to return os errors to you own code.
import Foundation
let task = Process()
let pipe = Pipe()
task.launchPath = "/bin/bash"
task.arguments = ["-c","unknown"]
task.standardOutput = pipe
task.launch()
let handle = pipe.fileHandleForReading
let data = handle.readDataToEndOfFile()
let dataString = String(data: data, encoding: .utf8)
print(dataString ?? "")
will print
/bin/bash: unknown: command not found
Handling error when using NSTask
ab
writes its error messages, including usage information, to standard error. You're currently only reading from standard output. To access the error messages or usage information you'll need to allocate a second NSPipe
, pass it to -[NSTask setStandardError:]
, and then read data from it.
NSTask launch path not accessible
What you will need to do is get the shell binary, and pass your script as an argument. So if the shell script is written targeting bash, get the bash interpreter, and pass it an argument list with one argument: the path to your script.sh.
Trying to run NSTask but getting an error
Here's a simple NSTask test that should work for you (note: arguments:(NSArray *)arguments):
/*
gcc -Wall -O3 -x objective-c -fobjc-exceptions -framework Foundation -o nstask nstask.m
./nstask
http://stackoverflow.com/questions/5081846/trying-to-run-nstask-but-getting-an-error
launchedTaskWithLaunchPath:arguments:
Creates and launches a task with a specified executable and arguments.
+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments
*/
#import <Foundation/Foundation.h>
int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSTask *task = [NSTask new];
NSMutableArray *kickBuild = [[NSMutableArray alloc] initWithCapacity:5];
//NSMutableArray *kickOut = [[NSMutableArray alloc] initWithCapacity:2];
NSString *kickPath = @"/bin/ls";
//[kickBuild addObject:@"/bin/ls"]; // Do I need this?
[kickBuild addObject: [@"~/Desktop" stringByExpandingTildeInPath]];
task = [NSTask launchedTaskWithLaunchPath: kickPath arguments: kickBuild];
[task waitUntilExit];
[pool release];
return 0;
}
NSTask not picking up $PATH from the user's environment
Running a task via NSTask uses fork()
and exec()
to actually run the task. The user's interactive shell isn't involved at all. Since $PATH
is (by and large) a shell concept, it doesn't apply when you're talking about running processes in some other fashion.
Error - NSTask with top command
Open Terminal app, and enter which top
.
OOPers-MacBook:~ dev$ which top
/usr/bin/top
Try with task.launchPath = "/usr/bin/top"
.
Launch path not accessible using NSTask to create Git commit
The task's launchPath
is the path to the program you want to run: that's Git here, so that path probably needs to be /usr/local/bin/git
. And remove @"git"
from the arguments; it's not an argument, it's the executable.
The path to your project should be used for the task's currentDirectoryPath
so that it has the correct working directory.
Using NSTask to execute ionic build commands - launch path not accessible
If you want to use bash
to launch ionic
, the equivalent command that you'll need to execute is /bin/bash -c ionic run
, thus you'll need to change the following:
- remove the leading
cd
fromcurrentDirectoryPath
(that's probably a typo from a copy+paste from terminal) and add a leading/
to have an absolute path - change
arguments
to@[@"-c",@"ionic",@"run"]
, as each argument to NSTask should represent an item of the array.
If you have problems with /usr/bin/bash
due to some other tools not being found, you can try using /bin/sh
.
Update As Aditya Vaidyam pointed out here you might also need to setup the environment variables (especially the PATH
one), to simulate the same conditions as for the terminal. If you want to find out which environment variables are set, just run the env
command.
Related Topics
How to Pass/Get Core Data Context in Swiftui Mvvm Viewmodel
How to Draw Dashed Line in Arkit (Scenekit) Like in the Measure App
Sending Firebase Push Notifications to Logged-In Users Only
Uisegment Value Changing When Tableview Get Scrolled
How to Find the Time Interval Remaining from Nstimer
Does Swift Have Short-Circuiting Higher-Order Functions Like Any or All
Swiftui [Bug] Navigationview and List Not Showing on iPad Simulator Only
How to Set Scrollview Content Size in Swift 3.0
What Are the Fundamental Differences Between Set and Didset
Swift: Cast Any Object to Int64 = Nil
Delegate Must Respond to Locationmanager:Didupdatelocations Swift Eroor
Does Nsnumberformatter.Stringfromnumber Ever Return Nil
Swipe Gesture Interrupts Uislider Control in iOS 13, But Not Previous iOS Versions
How to Declare, Create, and Use Method Pointers in Swift
Why Doesn't Swift Force My Designated Initializer to Call Super