Getting Process Information in Nu Scripts

Wednesday, 20 Aug 2008

The NSProcessInfo class is a handy resource for Nu scripts that need to know something about themselves and their operating environment. To get details on NSProcessInfo, check the online Apple documentation or just introspect the class in nush, the Nu shell:

% (puts ((NSProcessInfo instanceMethodNames) description))
% (puts ((NSProcessInfo instanceMethodNames) description))
(
    activeProcessorCount,
    arguments,
    dealloc,
    environment,
    globallyUniqueString,
    hostName,
    isTranslated,
    operatingSystem,
    operatingSystemName,
    operatingSystemVersionString,
    physicalMemory,
    processIdentifier,
    processName,
    processorCount,
    "setArguments:",
    "setProcessName:",
    userFullName,
    userHomeDirectory,
    userName
)
Those are the instance methods. There is just one class method; use it to get the “singleton” NSProcessInfo object:
% (puts ((NSProcessInfo classMethodNames) description))
(
    processInfo
)
From there on, it’s easy to see lots of interesting things about your process. Here are some examples:
% (set p (NSProcessInfo processInfo))
<NSProcessInfo:20fa10>

% (p activeProcessorCount)
8

% (puts ((p arguments) description))
(
    "/usr/local/bin/nush" 
)
()

% (puts ((p environment) description))
{
    EDITOR = vi;
    GROUP = staff;
    HOME = "/Users/tim";
    HOSTTYPE = "intel-pc";
    LOGNAME = tim;
    MACHTYPE = i386;
    MAIL = "/var/mail/tim";
    MANPATH = "/usr/share/man:/usr/local/share/man:/usr/X11/man";
    OSTYPE = darwin;
    PATH = ".:/Users/tim/bin:/usr/local/pgsql/bin:/usr/local/bin:...
    PWD = "/Users/tim";
    SHELL = "/bin/tcsh";
    SHLVL = 1;
    TERM = "xterm-color";
    USER = tim;
    VENDOR = apple;
    "__CF_USER_TEXT_ENCODING" = "0x3E8:0:0";
}
()

% (p globallyUniqueString)
"97F1F517-A895-4D40-8BF8-BFF24F0C07FE-86498-0002AC10790F5D7C" 

% (p hostName)
"Sulfur.local" 

% (p isTranslated)
0

% quit

tim% arch -ppc nush 
Nu Shell.

% (set p (NSProcessInfo processInfo))
<NSProcessInfo:30d4b0>

% (p isTranslated)
1

% quit

tim% nush
Nu Shell.

% (set p (NSProcessInfo processInfo))
<NSProcessInfo:20fa10>

% (p operatingSystem)
5

% (p operatingSystemName)
"NSMACHOperatingSystem" 

% (p operatingSystemVersionString)
"Version 10.5.1 (Build 9B2117)" 

% (p physicalMemory)
12884901888

% (p processIdentifier)
86554

% (p processName)
"nush" 

% (p processorCount)
8

% (p userFullName)
"Tim Burks" 

% (p userHomeDirectory)
"/Users/tim" 

% (p userName)
"tim" 
You can use this to write a “pid” file for a process with this brief bit of code:
((((NSProcessInfo processInfo) processIdentifier) stringValue)
 writeToFile:"myprocess.pid" atomically:NO)
Comments (1) post a reply
  1. steve Sunday, 21 Sep 2008, 02:16 AM PST

    More posts like this please! Even though it’s very simple, it shows how you intended Nu to be used.

    For example, I did not know about the (x description) applying to arrays, so I had always been typing the much lengthier loop to print each element of the array!

    It would be an interesting posting to go through your nush history and pull out interesting ways of how you examine the system and what Nu is doing.

    For example, how do you work with a program that has runtime Nu… digging through to get the button you want, and how do you set variables so you don’t have to type everything all over again the next time you run the program?

    How do you set the command line to act as though it is inside an object, so that the variables on the command line are the variables of the object? Or do you have another approach for handling run time objects?

    Just a few ideas. :)