-
Notifications
You must be signed in to change notification settings - Fork 8
/
check_and_update_pid.php
47 lines (41 loc) · 1.86 KB
/
check_and_update_pid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
require_once 'conn/conn.php';
// Function to check if the PID is running
function isPIDRunning($pidM3U8)
{
// Check if the PID exists and is running
exec('ps -p ' . $pidM3U8, $output);
return count($output) > 1;
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (isset($_GET['channelId']) && is_numeric($_GET['channelId'])) {
$channelId = $_GET['channelId'];
// Check if the channel exists in the database
$stmt = $db->prepare('SELECT * FROM canales WHERE id = :channelId');
$stmt->bindParam(':channelId', $channelId, PDO::PARAM_INT);
$stmt->execute();
$channel = $stmt->fetch(PDO::FETCH_ASSOC);
if ($channel) {
$pidM3U8 = $channel['pidm3u8'];
$timeStarted = $channel['time_started'];
if ($pidM3U8 && isPIDRunning($pidM3U8)) {
// If the PID exists and is running, return status as "running"
$currentTime = time();
$timeStartedTimestamp = strtotime($timeStarted);
$runningTime = $currentTime - $timeStartedTimestamp;
$hours = floor($runningTime / (60 * 60));
$minutes = floor(($runningTime % (60 * 60)) / 60);
$seconds = $runningTime % 60;
$timeRunning = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
echo json_encode(['status' => 'running', 'pid' => $pidM3U8, 'runningTime' => $timeRunning]);
exit;
} else {
// If the PID is not running or doesn't exist, return status as "stopped"
echo json_encode(['status' => 'stopped', 'pid' => null, 'runningTime' => null]);
exit;
}
}
}
}
// If the request is invalid or the channel doesn't exist, return an error response
echo json_encode(['status' => 'error', 'message' => 'Invalid channel ID']);