riccardolardi
riccardolardi

Reputation: 1741

Check if process is running from within cocoa app

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.

Upvotes: 2

Views: 1767

Answers (1)

Suhas
Suhas

Reputation: 1500

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];

Upvotes: 1

Related Questions