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 6617109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:39:55+00:00 2026-05-25T20:39:55+00:00

I want to execute top and get the result in order to know how

  • 0

I want to execute top and get the result in order to know how much memory is used by a process.

First, I want to execute a terminal command. I use code from this question
Execute a terminal command from a Cocoa app

I tried with uptime and it worked flawlessly.

But, when I tried to use top instead of uptime, i get the following error:

Error opening terminal: unknown.

Or, top is in /usr/bin/top, I don’t know why it refuses to use it.

Here is my code:

   NSTask *task;
   task = [[NSTask alloc] init];
   [task setLaunchPath: @"/usr/bin/top"];

   /*NSArray *arguments;
   arguments = [NSArray arrayWithObjects: @"foo", @"bar.txt", nil];
   [task setArguments: arguments];*/

   NSPipe *pipe;
   pipe = [NSPipe pipe];
   [task setStandardOutput: pipe];

   NSFileHandle *file;
   file = [pipe fileHandleForReading];

   [task launch];

   NSData *data;
   data = [file readDataToEndOfFile];

   NSString *string;
   string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
   [label setStringValue: string];

   [string release];
   [task release];

Second question: the thing I am trying to do is to know how much memory is used by Google Chrome.
Because it uses a process for each tab, it’s difficult to know how much memeroy in total it uses.

Problem 1: top changes its value every second, how can I get a value?
Problem 2: how can I search in the result of top in order to find only the processes I want (given that I know their names).

Thanks.

  • 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-25T20:39:55+00:00Added an answer on May 25, 2026 at 8:39 pm

    The problem is that commands like top needs a terminal (= window to interact with) to execute.

    This means they need a real window context, because they display the results dynamically in the window (clearing the window, displaying the output of “top” in the whole terminal window, and more important, update the display every N seconds, redrawing the whole terminal window)

    So as commands like top need a real “Terminal”, this is why, when using top with NSTask, it throw “Error opening terminal”.


    You may instead check in the manual page of top (man top) if there is an option to avoid dynamic paging of the command and instead output a single shot (without using ncurse and the terminal dynamic display) directly on stdout (maybe try top -l 1 — Don’t have my Mac here to try myself and confirm).

    Or a better solution is probably to dig around commands like ps (or others?) instead of top that will give you the same info but directly on stdout instead of as a dynamic display in a terminal.

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

Sidebar

Related Questions

I want to execute a program in memory on Windows. I do not want
I want to execute this query : INSERT INTO [Order] (FactorId, ProductId, Entity) VALUES((SELECT
I have asp.net generated html code. I want execute jQuery click function when user
I want to execute an application (executable ending with an .exe extension ) that
I want to execute a program which takes in 1 text file(say like Text.exe),
I want to execute a code helloword.cpp which takes in some argument from console
I want to execute a javascript on already loaded webpage in Webview. I am
I want to execute one mysql statement where I have a select that is
I want to execute a javascript function in a c# code. Here is what
i want to execute some commands using NSIS script , but for the commands

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.