Linux下如何查看系统启动时间和运行时间

  • Post author:
  • Post category:linux



转自:http://www.cnblogs.com/xd502djj/archive/2011/05/03/2035599.html


1.uptime命令


输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.00


2.查看/proc/uptime文件计算系统启动时间


cat /proc/uptime

输出: 5113396.94 575949.85

第一数字即是系统已运行的时间5113396.94 秒,运用系统工具date即可算出系统启动时间

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

输出: 2008-11-09 11:50:31


3.查看/proc/uptime文件计算系统运行时间

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)}'

输出:系统已运行:59天4时13分9秒

下面的摘自

http://www.cnblogs.com/kerrycode/p/3759395.html


1: who 命令查看

who -b 查看最后一次系统启动的时间。

who -r 查看当前系统运行时间

ysx-soft1@ubuntu:~$ who -r
         run-level 2  2019-01-15 09:59
ysx-soft1@ubuntu:~$ who -b
         system boot  2019-01-15 09:59


2: last  reboot

如下所示last reboot可以看到Linux系统历史启动的时间。 重启一下操作系统后,然后

[root@DB-Server ~]# last reboot

reboot

system boot 2.6.9-42.ELsmp Thu May 29 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

ysx-soft1@ubuntu:~$ last reboot
reboot   system boot  4.4.0-31-generic Tue Jan 15 09:59 - 10:03  (00:03)    
reboot   system boot  4.4.0-31-generic Mon Jan 14 15:53 - 10:03  (18:10)    
reboot   system boot  4.4.0-31-generic Sat Jan 12 19:02 - 10:03 (2+15:00)   
reboot   system boot  4.4.0-31-generic Wed Jan  9 21:26 - 19:00 (2+21:34)   
reboot   system boot  4.4.0-31-generic Tue Jan  8 07:08 - 19:00 (4+11:52)   
reboot   system boot  4.4.0-31-generic Mon Jan  7 07:05 - 19:00 (5+11:55)   
reboot   system boot  4.4.0-31-generic Sun Jan  6 21:16 - 19:00 (5+21:44)   
reboot   system boot  4.4.0-31-generic Thu Jan  3 22:24 - 21:12 (2+22:48)   
reboot   system boot  4.4.0-31-generic Thu Jan  3 18:57 - 21:12 (3+02:15)   

wtmp begins Wed Jan  2 10:19:43 2019

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

[root@DB-Server ~]# last reboot | head -1

reboot

system boot 2.6.9-42.ELsmp Thu May 29 15:25 (00:08)


3:TOP命令查看

如下截图所示,up后表示系统到目前运行了多久时间。反过来推算系统重启时间

ysx-soft1@ubuntu:~$ top
top - 10:04:17 up 4 min,  2 users,  load average: 0.19, 0.30, 0.15
Tasks: 297 total,   1 running, 296 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   2030820 total,   880592 used,  1150228 free,    78536 buffers
KiB Swap:  2094076 total,        0 used,  2094076 free.   332788 cached Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                      
  1500 root      20   0  231468   3936   2344 S   0.3  0.2   0:00.01 nmbd                         
  2638 ysx-sof+  20   0   29268   3076   2472 R   0.3  0.2   0:00.02 top                          
     1 root      20   0   34048   4576   2712 S   0.0  0.2   0:01.68 init                         
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kthreadd                     
     3 root      20   0       0      0      0 S   0.0  0.0   0:00.03 ksoftirqd/0                  
     4 root      20   0       0      0      0 S   0.0  0.0   0:00.21 kworker/0:0                  
     5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                 
     6 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/u256:0               
     7 root      20   0       0      0      0 S   0.0  0.0   0:00.15 rcu_sched                    
     8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh                       
     9 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 migration/0                  
    10 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 watchdog/0                   
    11 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 watchdog/1                   
    12 root      rt   0       0      0      0 S   0.0  0.0   0:00.00 migration/1                  
    13 root      20   0       0      0      0 S   0.0  0.0   0:00.00 ksoftirqd/1                  
    14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0                  
    15 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H                 
    16 root      20   0       0      0      0 S   0.0  0.0   0:00.01 kdevtmpfs   


4: w命令查看

如下截图所示,up后表示系统到目前运行了多久时间。反过来推算系统重启时间

ysx-soft1@ubuntu:~$ w
 10:05:09 up 5 min,  2 users,  load average: 0.08, 0.25, 0.14
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ysx-soft :0       :0               10:01   ?xdm?  19.31s  0.29s init --user
ysx-soft pts/0    192.168.31.240   10:03    0.00s  0.05s  0.00s w


5:uptime 命令查看

ysx-soft1@ubuntu:~$ uptime
 10:05:38 up 6 min,  2 users,  load average: 0.05, 0.23, 0.13
ysx-soft1@ubuntu:~$ 


6: 查看/proc/uptime

[root@DB-Server ~]# cat /proc/uptime

1415.59 1401.42

[root@DB-Server ~]# date -d “`cut -f1 -d. /proc/uptime` seconds ago”

Thu May 29 15:24:57 CST 2014

[root@DB-Server ~]# date -d “$(awk -F. ‘{print $1}’ /proc/uptime) second ago” +”%Y-%m-%d %H:%M:%S”


2014-05-29 15:24:57

 

下面的是程序的方式 ,在程序开发的时候会比较有用。摘自

http://www.cnblogs.com/Anker/p/3527609.html


1、前言

时间对操作系统来说非常重要,从内核级到应用层,时间的表达方式及精度各部相同。linux内核里面用一个名为jiffes的常量来计算时间戳。应用层有time、getdaytime等函数。今天需要在应用程序获取系统的启动时间,百度了一下,通过sysinfo中的uptime可以计算出系统的启动时间。


2、sysinfo结构

sysinfo结构保持了系统启动后的信息,主要包括启动到现在的时间,可用内存空间、共享内存空间、进程的数目等。man sysinfo得到结果如下所示:

复制代码

复制代码

 1 struct sysinfo {
 2                long uptime;             /* Seconds since boot */
 3                unsigned long loads[3];  /* 1, 5, and 15 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 64 bytes */
12            };


3、获取系统启动时间

通过sysinfo获取系统启动到现在的秒数,用当前时间减去这个秒数即系统的启动时间。程序如下所示:

 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)) {
13     fprintf(stderr, "Failed to get sysinfo, errno:%u, reason:%s\n",
14         errno, strerror(errno));
15     return -1;
16     }
17     time(&cur_time);
18     if (cur_time > info.uptime) {
19     boot_time = cur_time - info.uptime;
20     }
21     else {
22     boot_time = info.uptime - cur_time;
23     }
24     ptm = gmtime(&boot_time);
25     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);
27    return 0; 
28 }
29 
30 int main()
31 {
32     if (print_system_boot_time() != 0) {
33     return -1;
34     }
35     return 0;
36 }

测试结果如下所: