Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Last active January 16, 2024 21:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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(8);
$ps2 = '';
if ($pid2 = findPsPid($ps2)) {
if ($pid1 == $pid2) {
// `kill $pid1`;
echo $ps1;
if ($ps1 !== $ps2) {
echo "\n\n====\n\n" . $ps2;
}
}
}
}
return;
$test1 = <<<PS
3734200 node /home/forge/app.digitalinsightsplatform.nl/node_modules/lighthouse/cli/index.js --chrome-flags='--no-sandbox --headless --disable-gpu' --only-categories=performance --only-categories=accessibility --only-categories=seo --locale=nl --max-wait-for-load=12000 --throttling.cpuSlowdownMultiplier=2 --output=json --output-path=/tmp/dip/lighthouse264539209/desktop-0.json https://www.openscience-twente.com/ --preset=desktop
3734222 \_ /home/forge/.cache/puppeteer/chrome/linux-1095492/chrome-linux/chrome --disable-features=Translate,OptimizationHints,MediaRouter --disable-extensions --disable-component-extensions-with-background-pages --disable-background-networking --disable-component-update --disable-client-side-phishing-detection --disable-sync --metrics-recording-only --disable-default-apps --mute-audio --no-default-browser-check --no-first-run --disable-backgrounding-occluded-windows --disable-renderer-backgrounding --disable-background-timer-throttling --disable-ipc-flooding-protection --password-store=basic --use-mock-keychain --force-fieldtrials=*BackgroundTracing/default/ --remote-debugging-port=42725 --disable-setuid-sandbox --user-data-dir=/tmp/lighthouse.UFLIZ4g --no-sandbox --headless --disable-gpu about:blank
PS;
$test2 = <<<PS
2378935 /home/forge/.cache/puppeteer/chrome/linux-1095492/chrome-linux/chrome --disable-features=Translate,OptimizationHints,MediaRouter --disable-extensions --disable-component-extensions-with-background-pages --disable-background-networking --disable-component-update --disable-client-side-phishing-detection --disable-sync --metrics-recording-only --disable-default-apps --mute-audio --no-default-browser-check --no-first-run --disable-backgrounding-occluded-windows --disable-renderer-backgrounding --disable-background-timer-throttling --disable-ipc-flooding-protection --password-store=basic --use-mock-keychain --force-fieldtrials=*BackgroundTracing/default/ --remote-debugging-port=34285 --disable-setuid-sandbox --user-data-dir=/tmp/lighthouse.1WZkpL8 --no-sandbox --headless --disable-gpu about:blank
2378937 \_ /home/forge/.cache/puppeteer/chrome/linux-1095492/chrome-linux/chrome --type=zygote --no-zygote-sandbox --no-sandbox --headless --headless
PS;
var_dump(findPid($test1));
var_dump(findPid($test2));
function findPsPid(?string &$ps = null) {
$ps1 = `ps -axf -o pid,command | grep 'chrome\\|lighthouse' | grep -A1 -v '\\\\_'`;
// var_dump($ps1);
$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 {
// echo $ps;
$lines = array_filter(explode("\n", trim($ps ?? '')));
foreach ($lines as $i => $line) {
[$pid, $command] = splitLine($line);
// var_dump($pid, $command);
// echo "\n\n\n";
if (isRootChrome($command)) {
// echo "found chrome\n";
return $pid;
}
if (preg_match('#^node /home/forge/[^/]+/node_modules/lighthouse/cli/index\.js#', $command)) {
// echo "found index.js\n";
$nextLine = $lines[$i + 1] ?? null;
// echo "nextLine: $nextLine\n";
if ($nextLine) {
[$nextPid, $nextCommand] = splitLine($nextLine);
// echo "nextCommand: $nextCommand\n";
$rootedNextCommand = preg_replace('#^\\\\_ #', '', $nextCommand);
// echo "rootedNextCommand: $rootedNextCommand\n";
if (isRootChrome($rootedNextCommand)) {
// echo "found chrome\n";
return $nextPid;
}
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment