开发者

NSTask sending an argument through /bin/sh or /bin/bash problem Cocoa Objective-C

开发者 https://www.devze.com 2023-01-06 22:48 出处:网络
Basically I\'ve passed a script in the terminal through /bin/bash \"[path]\" or /bin/sh \"[path]\", and I\'ve passed commands in a termina开发者_开发技巧l through /bin/bash -c \"[command]\" or /bin/sh

Basically I've passed a script in the terminal through /bin/bash "[path]" or /bin/sh "[path]", and I've passed commands in a termina开发者_开发技巧l through /bin/bash -c "[command]" or /bin/sh -c "[command]". Both ways work properly. But in my Cocoa App, when I try to do the exact same thing with NSTask (using /bin/bash or /bin/sh), the app seems to never respond. It is as if the program is stuck inside of bash or sh. I've tried to do this with and without waitUntilExit, and I've also tried to use terminate. Still no luck. Has anyone else had this issue or have a clue as to why this is happening?

Thanks!

P.S. I'm not on my work computer right now, but, if needed, I can provide code later.


Ok. So I found the solution for anyone else that might need it. Basically, there is a problem is the standardInput. The problem only shows up in the Xcode console and is not an error/bug with your app. The fix is to add the following line (basically set the standardInput to something random):

[task setStandardInput: [NSPipe pipe]];

In the solution above, task is the variable name of the NSTask being used.


Also see:

http://www.cocoadev.com/index.pl?HowToPipeCommandsWithNSTask

[proc setLaunchPath:@"/bin/bash"];
[proc setArguments:[NSArray arrayWithObjects: @"-c", @"/usr/sbin/netstat -rn | /usr/bin/grep default", nil]];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号