<?php
declare(strict_types=1);

header('Content-Type: application/json');

$output = [];
$returnVar = 0;
exec('ps aux | grep "node index.js" | grep -v grep', $output, $returnVar);

$isRunning = !empty($output);
$pid = null;
if ($isRunning) {
    $parts = preg_split('/\s+/', trim($output[0]));
    $pid = $parts[1] ?? null;
}

echo json_encode([
    'success' => true,
    'running' => $isRunning,
    'pid' => $pid,
    'details' => $output
]);
