How to view system boot time and run time under Linux

Source: Internet
Author: User

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
Cat/proc/uptime
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:
[[email protected] ~]# date-d "$ (awk-f. ' {print $} '/proc/uptime) second ago" + "%y-%m-%d%h:%m:%s" 2017-10-27 14:32:35

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

Code:
[Email protected] ~]# cat/proc/uptime| Awk-f. ' {run_days=$1/86400;run_hour= ($ 86400)/3600;run_minute= ($ 3600)/60;run_second=$1% 60;printf ("system has been running:%d days% D (%d minutes%d seconds ", Run_days,run_hour,run_minute,run_second)} ' system has been running: 0 days 0:24 34 seconds

1: who command view

Who-b View the last time the system started.

[[email protected] ~]# Who-b         system boot  2017-10-27 14:32

Who-r View current system run time

[[email protected] ~]# who-r         run-level 3  2017-10-27 14:33

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

[Email protected] ~]# last rebootreboot   system boot  3.10.0-327.el7.x Fri Oct 14:32-14:59  (00:26)    wt MP begins Fri Oct 27 14:32:39 2017

#如果只需要查看最后一次Linux系统启动的时间

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

[[email protected] ~]# who-b system boot 2017-10-27 14:32top-15:00:29 up min, 2 users, load average: 0.00, 0.01, 0.05tasks:99 Total, 1 running, 98 sleeping, 0 stopped, 0 zombie%cpu (s): 0.0 us, 0.2 sy, 0.0 ni, 9 9.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stkib mem:8176008 Total, 7892856 free, 131764 used, 151388 Buff/cachekib S  wap:8257532 Total, 8257532 free, 0 used.                              7868548 avail Mem PID USER PR NI VIRT RES SHR S%cpu%MEM time+ COMMAND    1855 Root 0 0 0 0 S 0.3 0.0 0:00.33 kworker/0:2 1 root 20      0 44496 7168 2612 S 0.0 0.1 0:02.32 systemd 2 root 20 0 0  0 0 S 0.0 0.0 0:00.00 kthreadd 3 Root 0 0 0 0 S 0.0 0.0 0:00.05 ksoftirqd/0 5 root 0-20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0h 6 root 0 0 0 0 S 0.0 0.0 0:                            00.03 kworker/u4:0        7 Root RT 0 0 0 0 S 0.0 0.0 0:00.06 migration/0 8 root 20 0      0 0 0 S 0.0 0.0 0:00.00 rcu_bh 9 root 20 0 0 0   0 S 0.0 0.0 0:00.00 rcuob/0 Root 0 0 0 0 S 0.0 0.0 0:00.00 RCUOB/1 Root 0 0 0 0 S 0.0 0.0 0:00.33 Rcu_sche                                D Root 0 0 0 0 S 0.0 0.0 0:00.12 rcuos/0 Root 0 0 0 0 S 0.0 0.0 0:00.37 rcuos/1 RO OT RT 0 0 0 0 S 0.0 0.0 0:00.01 watchdog/0 15 Root RT 0 0 0 0 S 0.0 0.0 0:00.00 WATCHDOG/1 root RT 0      0 0 0 S 0.0 0.0 0:00.08 MIGRATION/1 root 20 0 0 0   0 S 0.0 0.0 0:00.00 ksoftirqd/1 root 0-20 0 0 0 S 0.0 0.0            0:00.00 kworker/1:0h

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

[Email protected] ~]# W 15:00:56 up-min,  2 users,  load average:0.00, 0.01, 0.05USER     TTY from             [EMA Il protected]   IDLE   jcpu   PCPU whatroot     tty1                      14:33   25:52   0.07s  0.07s-bashroot     pts/0    10.0.100.55      14:37    0.00s  0.10s  0.00s W

5:uptime Command View

[Email protected] ~]# uptime 15:01:40 up min,  2 users,  load average:0.00, 0.01, 0.05

6: View/proc/uptime

Method One:

[Email protected] ~]# cat/proc/uptime1778.05 3520.28

Method Two:

[Email protected] ~]# date-d "' cut-f1-d./proc/uptime ' seconds ago" Fri Oct 14:32:35 CST 2017

Method Three:

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

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.

When the application gets the startup time of the system, the startup time of the system can be calculated by uptime in SysInfo.

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:

How to view system boot time and run time under Linux

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: info-contact@alibabacloud.com 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.