Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6831375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:43:49+00:00 2026-05-26T22:43:49+00:00

Cocoa newbie warning! I find the following shell command to be a nice way

  • 0

Cocoa newbie warning!

I find the following shell command to be a nice way to determine if a process is running (1 = running, 0 = not running):

if [ $(ps -Ac | egrep -o 'ProcessName') ]; then echo 1; else echo 0; fi;

I can incorporate this into Cocoa with the “system” command:

system("if [ $(ps -Ac | egrep -o 'Finder') ]; then echo 1; else echo 0; fi;");

However, the output is directed to the run log, and I can’t figure out how to capture the result (1 or 0) in my Cocoa code.

I tried implementing this with NSTask as follows:

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObject:@"if [ $(ps -Ac | egrep -o 'Finder') ]; then echo 1; else echo 0; fi;"]];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task launch];
NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
[task release];
NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog (@"%@", output);
[output release];

However, this generates the following error message:

if [ $(ps -Ac | egrep -o ‘Finder’) ]; then echo 1; else echo 0; fi;: No such file or directory

Can you please tell me how I can correctly implement this shell command in a way that allows me to capture the output (1 or 0) in code? (I am aware of other methods of determining whether a process is running, but part of the reason for my question is to learn how to implement shell scripts in general within Cocoa.)

Thank you very much for any help with this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:43:50+00:00Added an answer on May 26, 2026 at 10:43 pm

    Rob, thank you for the general pointer about using a direct solution in code whenever possible. I looked through JongAm Park’s wrapper for sysctl, as well as Apple’s ps source code. It will take a while to incorporate, but I know now where to look for a direct solution to getting a list of processes.

    shellter and tripleee, thank you for your suggestions concerning interacting with shell commands. Based on your suggestions, I got three methods to work (!):

    Method 1 uses the system command’s return code (no need for egrep -o):

    BOOL processIsRunning = system("ps -Ac | grep 'ProcessName' > /dev/null") == 0;
    

    Method 2 uses NSTask with the shell -c option (note -c rather than -e):

    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/bin/sh"];
    [task setArguments:[NSArray arrayWithObjects:@"-c",@"ps -Ac | grep 'ProcessName'",nil]];
    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput:pipe];
    [task launch];
    NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
    [task waitUntilExit];
    [task release];
    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    BOOL processIsRunning = [result length] > 0;
    

    Method 3 also uses NSTask with the shell -c option, but in this case, the command to be executed is another shell with its own -c option (this was the only way I could find after much trial and error to incorporate an if construct in the command to be executed; of course, it is way overkill for the current problem):

    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/bin/sh"];
    [task setArguments:[NSArray arrayWithObjects:@"-c",@"/bin/sh -c 'if [ \"$(ps -Ac | egrep -o 'ProcessName')\" = \"ProcessName\" ]; then echo 1; else echo 0; fi;'",nil]];
    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput:pipe];
    [task launch];
    NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
    [task waitUntilExit];
    [task release];
    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    BOOL processIsRunning = [result intValue] == 1;
    

    Thank you all for the wonderful help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Cocoa newbie here... I know this sounds stupid but I can't find the Bindings
Warning: I'm a Cocoa newbie. I'm reading Cocoa Programming For Mac OS X by
I am newbie in cocoa. I am working on a GUI for a shell
Newbie Cocoa Touch question: I'm aiming for an interface with a UIPickerView and a
I am newbie with Cocoa Touch, I have a problem that I try to
I am a newbie to Cocoa, I have a few doubts regarding NSImage. Question1:
I'm a total newbie to programming in cocoa for the mac so this question
I am a cocoa newbie trying to create an iPhone app including Core Data.
I am a newbie in Cocoa and, while waiting for my copy of Aaron
I´m newbie. I have three xib files in my cocoa app: MainMenu.xib the main

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.