PHP 如何获取 Linux 服务器的 uptime

2023-04-15 10:20:46 浏览数 (1)

下面两种 PHP 方法都可以获取 Linux 服务器的 uptime:

代码语言:javascript复制
<?php
$uptime = trim(shell_exec('uptime'));
// output is 04:47:32 up 187 days,  5:03,  1 user,  load average: 0.55, 0.55, 0.54

$uptime = explode(',', $uptime);
$uptime = explode(' ', $uptime[0]);

$uptime = $uptime[2].' '.$uptime[3]; // 187 days
?>
代码语言:javascript复制
<?php
$uptime = trim(file_get_contents('/proc/uptime'));
$uptime = explode(' ', $uptime);

echo $uptime[0]; //uptime in seconds
?>

0 人点赞