0

I am trying a linux command for uptime in a php file on a server as below

<p> The uptime for this system is <?php $up_time=shell_exec('uptime');
echo $uptime; ?</p>

But when I open my webpage it just shows :

The uptime for this system is

PHP doesn't appear to be running the command. If I go to command prompt and run uptime, I do see the results.

I am doing ssh into a Linux webserver. The php file resides in my folder on the server.

2 Answers2

0

I think the following code will work.

The uptime for this system is <?php passthru('/usr/bin/uptime'); ?>

executing system command in a php, I suggest you always provide the full path of the command.

Xing Fei
  • 287
  • 1
  • 6
  • Hi - Thanks a lot. It worked but there is a difference between what I get from the php command and what I get if I write uptime on the terminal. In php I get the following "The uptime for this system is 00:21:47 up 77 days, 9:58, 0 users, load average: 0.00, 0.00, 0.00" But on the terminal I get the following "00:21:42 up 755 days, 22:16, 4 users, load average: 0.00, 0.00, 0.00 " would you know why? –  Apr 24 '14 at 03:20
  • do you execute commands on the same machine? 77days <<< 755days! – Xing Fei Apr 24 '14 at 07:20
0

Linux server uptime can be easily obtained in PHP using the /proc/uptime

$uptimeseconds = (int)shell_exec('cat /proc/uptime');
echo "This server has been running for $uptimeseconds seconds.\n";
helvete
  • 2,455
  • 13
  • 33
  • 37