Production of Linux run time

Source: Internet
Author: User

How to view system boot time and run time under Linux

1.uptime command
Output: 16:11:40 up, 4:21, 2 users, load average:0.00, 0.01, 0.00

2. View the/proc/uptime file calculation system start time
Output: 5113396.94 575949.85
The first number is the system has been running for 5,113,396,.94 seconds, using the System tool date to calculate the system boot time

Code: Select All
date -d "$(awk -F. ‘{print $1}‘ /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"

Output: 2008-11-09 11:50:31

3. View/proc/uptime file calculation system run time

Code: Select All
cat /proc/uptime| awk -F. ‘{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_second=$1 % 60;printf("系统已运行:%d天%d时%d分%d秒",run_days,run_hour,run_minute,run_second)}‘

Output: System has been running: 59 days 4:13 9 seconds

The following excerpt from

1:who Command View

Who-b View the last time the system started.

Who-r View current system run time

[Email protected] ~]# Who-b

System Boot May 11 09:27

2:last reboot

As shown in the last reboot, you can see the time the Linux system history started. After restarting the operating system, then

[Email protected] ~]# last reboot


System Boot 2.6.9-42.elsmp Thu may 15:25 (00:07)

Reboot system boot 2.6.9-42.ELSMP Sun May 11 09:27 (18+05:55)

Wtmp begins Mon May 5 16:18:57 2014

If you only need to see when the last Linux system started

[Email protected] ~]# last reboot | Head-1


System Boot 2.6.9-42.elsmp Thu may 15:25 (00:08)

3:top Command View

As shown below, up indicates how long the system has been running for the time being. In turn, the system restart time is calculated

4:w Command View

4:w Command View

As shown below, up indicates how long the system has been running for the time being. In turn, the system restart time is calculated

5:uptime Command View

6: View/proc/uptime

[Email protected] ~]# Cat/proc/uptime

1415.59 1401.42

[Email protected] ~]# date-d "' cut-f1-d./proc/uptime ' seconds Ago"

Thu 15:24:57 CST 2014

[[email protected] ~]# date-d "$ (awk-f. ' {print $} '/proc/uptime) second ago" + "%y-%m-%d%h:%m:%s"

2014-05-29 15:24:57

The following is the way the program, in the development of the program will be more useful. Excerpted from

1. Preface

Time is very important to the operating system, from the kernel level to the application layer, the expression of time and the accuracy of each department is the same. The Linux kernel uses a constant called Jiffes to calculate the timestamp. The application layer has time, getdaytime and other functions. Today needs to get the system start time in the application, Baidu a bit, through the sysinfo in the uptime can calculate the system start time.

2. SysInfo structure

The SYSINFO structure maintains the information of the system startup, including the time of startup to the present, the available memory space, the shared memory space, the number of processes, etc. The man sysinfo gets the result as follows:

1 struct SysInfo {2                long uptime;             /* Seconds since Boot */3                unsigned long loads[3];  /* 1, 5, and minute load averages */4                unsigned long totalram;  /* Total usable main memory size */5                unsigned long freeram;   /* Available Memory Size *                /6 unsigned long sharedram;/* Amount of Shared memory *                /7 unsigned long bufferram; * Memory used by buffers */8                unsigned long totalswap;/* Total swap space size */9                unsigned long freeswap;
    /* swap space still available */10                unsigned short procs;    /* Number of current processes */11                char _f[22];             /* Pads structure to bytes */12            };

3. Get the system boot time

SysInfo gets the number of seconds that the system has booted to the present time, minus the number of seconds in the current period, which is the system startup time. The program looks like this:

  1 #include <stdio.h> 2 #include <sys/sysinfo.h> 3 #include <time.h> 4 #include <errno.h > 5 6 static int print_system_boot_time () 7 {8 struct sysinfo info; 9 time_t cur_time = 0;10 time_t boot_ Time = 0;11 struct TM *ptm = null;12 if (sysinfo (&info)) {fprintf, "stderr to get Failed, SysInfo :%u, reason:%s\n ", errno, Strerror (errno)), return-1;16}17 Time (&cur_time), if (Cur_ti Me > Info.uptime) {boot_time = cur_time-info.uptime;20}21 else {boot_time = Info.uptime-cur_  time;23}24 ptm = gmtime (&boot_time); printf ("System boot time:%d-%-d-%d%d:%d:%d\n", Ptm->tm_year + 1900,26 Ptm->tm_mon + 1, ptm->tm_mday, Ptm->tm_hour, Ptm->tm_min, ptm->tm_sec); return 0; }29 int main () {print_system_boot_time ()! = 0) {return-1;34}35 return 0;36}  /pre>

The test results are as follows:

Tags: Linux

Production of Linux run time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.