Created
May 24, 2012 23:35
-
-
Save syropian/2784880 to your computer and use it in GitHub Desktop.
Checks if the specififed process is active. Works on all processes (standard and background)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -(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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment