Professional Web Applications Themes

starting a command line app from code - Mac Programming

I want to start a command line program (deamon) from my Cocoa app. It seems there are a lot of ways to do this. I found at least four: - (void)startDeamon { NSString *execPath = "path/to/executable"; NSString *execName = [execPath lastPathComponent]; // (1) LaunchServices NSURL *deamonURL = [NSURL fileURLWithPath:execPath]; LSOpenCFURLRef((CFURLRef)deamonURL, NULL); // (2) use system() NSString *command = [execPath stringByAppendingString:"&"]; system([command fileSystemRepresentation]); // (3) the unix solution if ( fork() == 0 ) { // in child const char *path = [execPath fileSystemRepresentation]; const char *name = [execName fileSystemRepresentation]; execl(path, name, NULL); } // (4) NSTask one liner [NSTask launchedTaskWithLaunchPath:execPath ...

  1. #1

    Default starting a command line app from code

    I want to start a command line program (deamon) from my Cocoa app. It
    seems there are a lot of ways to do this. I found at least four:

    - (void)startDeamon
    {
    NSString *execPath = "path/to/executable";
    NSString *execName = [execPath lastPathComponent];

    // (1) LaunchServices
    NSURL *deamonURL = [NSURL fileURLWithPath:execPath];
    LSOpenCFURLRef((CFURLRef)deamonURL, NULL);

    // (2) use system()
    NSString *command = [execPath stringByAppendingString:"&"];
    system([command fileSystemRepresentation]);

    // (3) the unix solution
    if ( fork() == 0 ) {
    // in child
    const char *path = [execPath fileSystemRepresentation];
    const char *name = [execName fileSystemRepresentation];
    execl(path, name, NULL);
    }

    // (4) NSTask one liner
    [NSTask launchedTaskWithLaunchPath:execPath arguments:[NSArray
    array]];
    // not sure about 'arguments' here...
    }

    Can anyone shed some light on the relative merits of these alternatives?
    Are there any side effects I should be aware of?

    Patrick
    ---
    Hieper Software

    w: www.hieper.nl
    e: nl
    Patrick Guest

  2. #2

    Default Re: starting a command line app from code

    Patrick Machielse wrote:
     

    Do you want to just run a program, or really start a daemon?
    They are different. We usually want daemons to stay around
    after we leave; we usually want background processes to quit
    when we log off.
     

    Need more work here to keep us around,
    if you really want a daemon.
     

    And alternative 3b, using the daemon() call:

    int daemon(int nochdir, int noclose);




    Mike Guest

  3. #3

    Default Re: starting a command line app from code

    Mike Hall <com> wrote:
     
    >
    > Do you want to just run a program, or really start a daemon?
    > They are different. We usually want daemons to stay around
    > after we leave; we usually want background processes to quit
    > when we log off.[/ref]

    In that case, what I want is a faceless 'background process' :-) I
    didn't realize they were different. The process should be kicked when
    the user logs out.
     
    >
    > Need more work here to keep us around,
    > if you really want a daemon.[/ref]

    What would be needed? (this code launches my process OK) The 'daemon'
    itself uses [[NSRunloop defaultRunloop] run] to stay alive.
     
    >
    > And alternative 3b, using the daemon() call:
    >
    > int daemon(int nochdir, int noclose);[/ref]

    Ah! I don't have a *nix programming background, so than these calls are
    hard to find. I probly don't want this anyway.

    I have a combination of a preference panel and a background app. The
    background app should be launched by either the pref panel or at login.
    I accomplish the last feat by setting an AppleScript as login item to
    launch my background app. (is there clean way to make a command line
    program a loginitem, a way that won't launch the terminal?)

    So what is the best way to launch a background process from a cocoa
    application?

    Patrick
    Patrick Guest

  4. #4

    Default Re: starting a command line app from code

    In article <1gh6wsi.1y1kw1wqak464N%nl>,
    nl (Patrick Machielse) wrote:
     

    Given that you don't have a Unix programming background, you're probably
    best off using NSTask to start up your process. There are a lot of
    subtleties that can come into play with fork/exec, but NSTask handles
    those for you. There's nothing wrong with using a more Unix-centered
    approach, except that as with so much in Unix it relies on you really
    knowing what you're doing.

    --
    Tom "Tom" Harrington
    Macaroni, Automated System Maintenance for Mac OS X.
    Version 2.0: Delocalize, Repair Permissions, lots more.
    See http://www.atomicbird.com/
    Tom Guest

Similar Threads

  1. Starting MySQL from the Windows Command Line
    By Pat Serrand in forum MySQL
    Replies: 6
    Last Post: February 14th, 01:44 AM
  2. RUN/execute a Command-Line command from an ASP page
    By Lucas Cowald in forum ASP.NET Building Controls
    Replies: 1
    Last Post: October 22nd, 04:26 PM
  3. RUN/execute a Command-Line command from an ASP page.
    By Lucas Cowald in forum ASP Components
    Replies: 7
    Last Post: October 22nd, 01:08 PM
  4. Using Pen Tool - Starting a new line?
    By BG in forum Macromedia Fireworks
    Replies: 0
    Last Post: July 30th, 01:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139