syropian
5/24/2012 - 11:35 PM

Checks if the specififed process is active. Works on all processes (standard and background)

Checks if the specififed process is active. Works on all processes (standard and background)

-(BOOL)processIsRunning:(NSString *)aProcess{
    NSTask* task = [[NSTask alloc] init];
    [task setLaunchPath: @"/usr/bin/top"];
    
    NSArray* arguments = [NSArray arrayWithObjects: @"-s", @"1",@"-l",@"3600",@"-stats",@"pid,cpu,time,command", nil];

    [task setArguments: arguments];
    
    NSPipe* pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];

    [task setStandardInput:[NSPipe pipe]];
    
    [task launch];
    
    NSData* processData = [[[task standardOutput]fileHandleForReading]availableData];
    NSString* processes = [[NSString alloc]initWithData:processData encoding:NSUTF8StringEncoding];
    if ([processes rangeOfString:aProcess].location != NSNotFound) {
        return TRUE;
    }
    else {
        return FALSE;
    }
}