+ Post New Thread
Results 1 to 7 of 7
*nix Thread, Uptime Command in Technical; I know uptime will give the user the uptime in a format of: [CURRENT TIME][UPTIME][USERS ONLINE][LOAD AVERAGE] But how do ...
  1. #1

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241

    Uptime Command

    I know uptime will give the user the uptime in a format of:

    [CURRENT TIME][UPTIME][USERS ONLINE][LOAD AVERAGE]

    But how do I get it so it only returns the UPTIME part?

  2. #2

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,803
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224
    Hacking up the line with the 'cut' command does the job.

    Code:
    uptime | cut -d' '  -f3-6
    the other option is to take the info directly from /proc/uptime.

    Edit:Bah too slow!

  3. #3

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    I just tried cat /proc/uptime and got:

    96789.49 95483.24


  4. #4

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    That's because uptime isn't stored in a human-readable format. Either use uptime and cut, or the small shell script in that article.

  5. #5

    Geoff's Avatar
    Join Date
    Jun 2005
    Location
    Fylde, Lancs, UK.
    Posts
    11,803
    Thank Post
    110
    Thanked 583 Times in 504 Posts
    Blog Entries
    1
    Rep Power
    224
    Quote Originally Posted by Hightower View Post
    I just tried cat /proc/uptime and got:

    96789.49 95483.24

    The first is the uptime in seconds of the system. The second is the total idle time of the system since boot.

    Consider the following C++ program.

    Code:
    #include <iostream>
    #include <fstream>
    #include <map>
    #include <stdio.h>
    #include <string>
    
    using namespace std;
    
    struct UptimeStats {
    
    	int nTotalSeconds;
    	
    	int nSeconds;
    	int nMinutes;
    	int nHours;
    	int nDays;
    	// years typically aren't presented.  "400 days" instead of "1 year, 35 days"
    
    };
    
    int main ( )
    {
    
    	ifstream uptime ( "/proc/uptime" );
    
    	double nUptimeSecondsDouble;
    
    	// Have to read it in as a double, or the next read is messed up
    	uptime >> nUptimeSecondsDouble;
    	int nUptimeSeconds = (int)nUptimeSecondsDouble;
    
    	printf ( "%d\n", nUptimeSeconds );
    
    	// create the UptimeStats object
    	UptimeStats oUptimeStats;
    	
    	oUptimeStats.nTotalSeconds = nUptimeSeconds;
    	
    	oUptimeStats.nSeconds = nUptimeSeconds % 60;
    	oUptimeStats.nMinutes = nUptimeSeconds / 60 % 60;
    	oUptimeStats.nHours = nUptimeSeconds / 3600 % 24;
    	oUptimeStats.nDays = nUptimeSeconds / 86400;
    	
    	printf ( "%d days %d hours %d minutes %d seconds\n",
    			 oUptimeStats.nDays,
    			 oUptimeStats.nHours,
    			 oUptimeStats.nMinutes,
    			 oUptimeStats.nSeconds );
    
    	///////////////////////////////////////////////////////////////////////////
    	// Same thing again to handle idle seconds
    	///////////////////////////////////////////////////////////////////////////
    	
    	int nIdleSecondsDouble;
    
    	uptime >> nIdleSecondsDouble;
    	
    	int nIdleSeconds = (int) nIdleSecondsDouble;
    
    	printf ( "%d\n", nIdleSeconds );
    
    	// create the UptimeStats object
    	UptimeStats oIdleStats;
    	
    	oIdleStats.nTotalSeconds = nIdleSeconds;
    	
    	oIdleStats.nSeconds = nIdleSeconds % 60;
    	oIdleStats.nMinutes = nIdleSeconds / 60 % 60;
    	oIdleStats.nHours = nIdleSeconds / 3600 % 24;
    	oIdleStats.nDays = nIdleSeconds / 86400;
    	
    	printf ( "%d days %d hours %d minutes %d seconds\n",
    			 oIdleStats.nDays,
    			 oIdleStats.nHours,
    			 oIdleStats.nMinutes,
    			 oIdleStats.nSeconds );
    
    
    }

  6. #6

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    Ok, I've just quickly jotted together this function:

    PHP Code:
    function uptime(){
        
    //Get the raw uptime (in seconds) of the Linux system
        
    $seconds shell_exec("cat /proc/uptime | cut -d'.' -f1-1");
        
    //Turn the number of seconds into days
        
    $seconds = (($seconds 60) / 60) / 24;

        
    //Return the number of days to the system
        
    return $seconds;

    This successfully returns the amount of uptime in days such as:

    Code:
    37.1237865
    What would be the easiest way to make this a whole number rather than one with decimals?

  7. #7

    Hightower's Avatar
    Join Date
    Jun 2008
    Location
    Cloud 9
    Posts
    4,920
    Thank Post
    494
    Thanked 690 Times in 444 Posts
    Rep Power
    241
    NM - just used the round() function.

SHARE:
+ Post New Thread

Similar Threads

  1. cmd command
    By tabby1039 in forum Windows
    Replies: 4
    Last Post: 3rd June 2008, 10:47 AM
  2. Blocking Command.com
    By Jamie_a in forum Windows
    Replies: 12
    Last Post: 11th October 2007, 09:31 AM
  3. Replies: 1
    Last Post: 10th September 2007, 12:42 PM
  4. how to disable command.com ?
    By pinemarten in forum How do you do....it?
    Replies: 17
    Last Post: 15th May 2007, 11:24 PM
  5. Command LIne
    By wesleyw in forum Windows
    Replies: 4
    Last Post: 12th October 2006, 11:10 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •