0

I have to run the command to kill the process and the command is kill pgrep -f /dev,I know that to run the command NSTask is used but how to run the above command with these special arguments and the pgrep -f /dev is in `` I have tried this code

NSTask *task = [[NSTask alloc]init];
 [task setLaunchPath:@"/bin/kill"];
 [task setArguments:@[@"`",@"pgrep",@"-f",@"/dev",@"`" ];
 [task launch];

Please tell how to write it properly,I know I have given arguments wrong. Thanks, Any help will be highly appreciated.

  • Check this one http://stackoverflow.com/questions/412562/execute-a-terminal-command-from-a-cocoa-app – Boda Mar 07 '16 at 09:23
  • I followed this link but still face the problem , how can i write a command sudo kill `pgrep -f /dev` using NSTask ? –  Mar 07 '16 at 09:54

1 Answers1

0

Your problem appears to lie in misunderstanding how a command line is handled. When you type the line:

kill `pgrep -f /dev`

you are typing to your shell, which is probably bash but could be tcsh etc. The back quotes (`) are a shell feature; the shell will arrange to run both commands and take the output of pgrep and pass it as arguments to kill.

In your code you are attempting to execute kill and pass it as arguments the back quoted pgrep command. This won't work, kill knows nothing about back quotes, that is a shell feature...

So read the shell documentation (man sh) and you will discover that you can pass a shell command line as a single argument to the shell command itself - you can try it in the terminal yourself. Having figured that out you can use your NSTask code to run the shell, passing it your command line.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86