2

I am a cocoa beginner and am trying to build an app for personal use, to manage local Apache/MySQL processes. I want to check if httpd (/usr/sbin/httpd) is running. I searched and found some hints pointing to NSTask and isRunning method, but could not get it to run.

How can I check to see if this process is running?

Besides, is this a reliable way to check whether OSX built-in Apache is running?

Thanks for any help.

riccardolardi
  • 1,713
  • 5
  • 20
  • 35

1 Answers1

1

Yes NSTask is the reliable way to check or launch other processes. The following code must help you achieve what you need.

NSTask *task = [[NSTask alloc] init];

[task setLaunchPath:@"/usr/sbin/httpd"];

if([task isRunning])
{
    NSLog(@"Hurray Apache running!!");
}

//If task is not running launch it yourself.

else 
{
    [task launch];

    if([task isRunning])
    {
        NSLog(@"Hurray Apache running!!");
    }
}

[task release];
Suhas
  • 1,500
  • 11
  • 15
  • Sadly, that simple approach doesnt seem to work in the case of /usr/sbin/httpd ... [task isRunning] returns NO ... I'm working around it by launching **bold**/bin/sh -c /bin/ps aux | grep httpd**bold** and checking whether the output includes a process with path /usr/sbin/httpd ... I know, rather a hack than a solution but ok for the time being. – riccardolardi Aug 02 '12 at 10:39