Created
April 18, 2019 05:56
-
-
Save danneu/5d805cf7349ff2c1c716a9397f12cd69 to your computer and use it in GitHub Desktop.
This file contains 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
// $ ps -c -Ao pid,comm,pcpu -r | tail -n +2 | head -n 5 | |
// 2333 installd 61.8 | |
// 2671 iTerm2 17.3 | |
// 2334 storedownloadd 4.1 | |
// 203 WindowServer 2.8 | |
// 5243 Activity Monitor 2.7 | |
func getTopCpuProcs() { | |
let ps = Process() | |
ps.launchPath = "/usr/bin/env" | |
ps.arguments = ["ps", "-c", "-Ao", "pid,comm,pcpu,time", "-r"] | |
let psOut = Pipe() | |
ps.standardOutput = psOut | |
let tail = Process() | |
tail.launchPath = "/usr/bin/env" | |
tail.arguments = ["tail", "-n", "+2"] | |
tail.standardInput = psOut | |
let tailOut = Pipe() | |
tail.standardOutput = tailOut | |
let head = Process() | |
head.launchPath = "/usr/bin/env" | |
head.arguments = ["head", "-n", "5"] | |
head.standardInput = tailOut | |
let headOut = Pipe() | |
head.standardOutput = headOut | |
// Trim leading whitespace | |
let sed1 = Process() | |
sed1.launchPath = "/usr/bin/env" | |
sed1.arguments = ["sed", #"s/^ *//g"#] | |
sed1.standardInput = headOut | |
let sed1Out = Pipe() | |
sed1.standardOutput = sed1Out | |
// Smush consecutive spaces into commas | |
let sed2 = Process() | |
sed2.launchPath = "/usr/bin/env" | |
sed2.arguments = ["sed", #"s/ \{1,\}/,/g"#] | |
sed2.standardInput = sed1Out | |
let sed2Out = Pipe() | |
sed2.standardOutput = sed2Out | |
ps.launch() | |
tail.launch() | |
head.launch() | |
sed1.launch() | |
sed2.launch() | |
sed2.waitUntilExit() | |
let data = sed2Out.fileHandleForReading.readDataToEndOfFile() | |
let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) | |
if output!.length == 0 { | |
print("no output") | |
return | |
} | |
// Parse it | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment