Welcome to Soft32 Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

starting a command line app from code

 
   Soft32 Home -> Mac -> Programmer Help RSS
Next:  getting a user's trash folders  
Author Message
Patrick Machielse

External


Since: Aug 16, 2003
Posts: 48



(Msg. 1) Posted: Mon Jul 19, 2004 8:43 am
Post subject: starting a command line app from code
Archived from groups: comp>sys>mac>programmer>help (more info?)

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: info RemoveThis @hieper.nl
Back to top
Login to vote
Mike Hall

External


Since: Dec 18, 2003
Posts: 25



(Msg. 2) Posted: Mon Jul 19, 2004 11:49 am
Post subject: Re: starting a command line app from code [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Patrick Machielse wrote:

>I want to start a command line program (deamon)

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.

> // (3) the unix solution
> if ( fork() == 0 ) {
> // in child

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

> const char *path = [execPath fileSystemRepresentation];
> const char *name = [execName fileSystemRepresentation];
> execl(path, name, NULL);
> }

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

int daemon(int nochdir, int noclose);
Back to top
Login to vote
Patrick Machielse

External


Since: Aug 16, 2003
Posts: 48



(Msg. 3) Posted: Mon Jul 19, 2004 7:37 pm
Post subject: Re: starting a command line app from code [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Mike Hall <mghallNO RemoveThis @SPAMenteract.com> wrote:

> Patrick Machielse wrote:
>
> >I want to start a command line program (deamon)
>
> 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.

In that case, what I want is a faceless 'background process' Smile I
didn't realize they were different. The process should be kicked when
the user logs out.

> > // (3) the unix solution
> > if ( fork() == 0 ) {
> > // in child
>
> Need more work here to keep us around,
> if you really want a daemon.

What would be needed? (this code launches my process OK) The 'daemon'
itself uses [[NSRunloop defaultRunloop] run] to stay alive.

> > const char *path = [execPath fileSystemRepresentation];
> > const char *name = [execName fileSystemRepresentation];
> > execl(path, name, NULL);
> > }
>
> And alternative 3b, using the daemon() call:
>
> int daemon(int nochdir, int noclose);

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
Back to top
Login to vote
Tom Harrington

External


Since: Aug 19, 2003
Posts: 1921



(Msg. 4) Posted: Tue Jul 20, 2004 4:37 am
Post subject: Re: starting a command line app from code [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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

> 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?

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/
Back to top
Login to vote
Display posts from previous:   
Related Topics:
GUI wrapper for command line application - Hi, I'm trying to write a GUI wrapper for a command line application and am following Apple's Moriarity example. Laun...

Command-line arguments in Mac App - I'm porting a Unix program to Mac OS X. I've made a bundle for the app, and I'd like to be able to have the executabl...

accessing command line arguments in AppKit? - I'm sure that this has got to be easy to do, I just can't find it. But I have a cocoa app (GUI/AppKit) that I'd like t...

Printing a view from code command P - Hello NG, I can't figure out how I shoud print a view out from code. I have a webview inside of a splitview and if I ...

Command line compiler arguments to build a framework? - What arguments do I need to pass the command line compiler in order to build a file as a framework?

Shell command line to set Login startup items - What is the shell command line to set Login startup items (like in Account System Preferences) ?
       Soft32 Home -> Mac -> Programmer Help All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum

Categories:
 Windows
 Linux
  Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]