Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Last active August 16, 2024 17:09
Show Gist options
  • Save rudiedirkx/4e10fff6628f19fb78c469f23543ac93 to your computer and use it in GitHub Desktop.
Save rudiedirkx/4e10fff6628f19fb78c469f23543ac93 to your computer and use it in GitHub Desktop.
<?php
// $ps = `ps -axf -o pid,command | grep -C3 '\/chrome\/'`;
// $ps = `ps -axf -o pid,command | grep 'chrome' | grep -C1 -v '\\_'`;
$ps1 = '';
if ($pid1 = findPsPid($ps1)) {
sleep(20);
$ps2 = '';
if ($pid2 = findPsPid($ps2)) {
if ($pid1 == $pid2) {
`kill $pid1`;
exit;
echo $ps1;
if ($ps1 !== $ps2) {
echo "\n\n====\n\n" . $ps2;
}
}
}
}
function findPsPid(?string &$ps = null) {
$ps1 = `ps -axf -o pid,command | grep 'chrome\\|lighthouse' | grep -A1 -v '\\\\_'`;
$pid = $ps1 ? findPid($ps1) : null;
if ($pid) {
$ps = $ps1;
return $pid;
}
return null;
}
function isRootChrome(string $command) : bool {
return strpos($command, '/home/forge/.cache/puppeteer/chrome/linux-') === 0;
}
function splitLine(string $line) : array {
[$pid, $command] = preg_split('#\\s+#', trim($line), 2);
return [$pid, $command];
}
function findPid(string $ps) : ?int {
$lines = array_filter(explode("\n", trim($ps ?? '')));
foreach ($lines as $i => $line) {
[$pid, $command] = splitLine($line);
if (isRootChrome($command)) {
return $pid;
}
if (preg_match('#^node /home/forge/[^/]+/node_modules/lighthouse/cli/index\.js#', $command)) {
$nextLine = $lines[$i + 1] ?? null;
if ($nextLine) {
[$nextPid, $nextCommand] = splitLine($nextLine);
$rootedNextCommand = preg_replace('#^\\\\_ #', '', $nextCommand);
if (isRootChrome($rootedNextCommand)) {
return $nextPid;
}
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment