zoukankan      html  css  js  c++  java
  • Linux下如何查看系统启动时间和运行时间以及安装时间

    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即可算出系统启动时间

    代码

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

    2017-10-27 14:32:35

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

    代码

    [root@localhost ~]# 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)}'

    系统已运行:0天0时24分34秒

    1: who 命令查看

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

     

     

    [root@localhost ~]# who -b

             system boot  2017-10-27 14:32

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

     

     

    [root@localhost ~]# who -r

             run-level 3  2017-10-27 14:33

    2: last  reboot

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

    [root@DB-Server ~]# last reboot

     

     

     

     

    [root@localhost ~]# last reboot

    reboot   system boot  3.10.0-327.el7.x Fri Oct 27 14:32 - 14:59  (00:26)   

    wtmp begins Fri Oct 27 14:32:39 2017

    #如果只需要查看最后一次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后表示系统到目前运行了多久时间。反过来推算系统重启时间

    [root@localhost ~]# who -b

             system boot  2017-10-27 14:32

    top - 15:00:29 up 27 min,  2 users,  load average: 0.00, 0.01, 0.05

    Tasks:  99 total,   1 running,  98 sleeping,   0 stopped,   0 zombie

    %Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st

    KiB Mem :  8176008 total,  7892856 free,   131764 used,   151388 buff/cache

    KiB Swap:  8257532 total,  8257532 free,        0 used.  7868548 avail Mem

      PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                            

     1855 root      20   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      20   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      20   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                            

       10 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/1                            

       11 root      20   0       0      0      0 S   0.0  0.0   0:00.33 rcu_sched                          

       12 root      20   0       0      0      0 S   0.0  0.0   0:00.12 rcuos/0                            

       13 root      20   0       0      0      0 S   0.0  0.0   0:00.37 rcuos/1                            

       14 root      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                         

       16 root      rt   0       0      0      0 S   0.0  0.0   0:00.08 migration/1                        

       17 root      20   0       0      0      0 S   0.0  0.0   0:00.00 ksoftirqd/1                        

       19 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H           

    4: w命令查看

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

    1

    2

    3

    4

    5

    [root@localhost ~]# w

     15:00:56 up 28 min,  2 users,  load average: 0.00, 0.01, 0.05

    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT

    root     tty1                      14:33   25:52   0.07s  0.07s -bash

    root     pts/0    10.0.100.55      14:37    0.00s  0.10s  0.00s w

    5:uptime 命令查看

    [root@localhost ~]# uptime

     15:01:40 up 29 min,  2 users,  load average: 0.00, 0.01, 0.05

    6: 查看/proc/uptime

    方法一:

    [root@localhost ~]# cat /proc/uptime

    1778.05 3520.28

    方法二:

    [root@localhost ~]# date -d "`cut -f1 -d. /proc/uptime` seconds ago"

    Fri Oct 27 14:32:35 CST 2017

     方法三:

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

    2017-10-27 14:32:35

     

    1、前言

      时间对操作系统来说非常重要,从内核级到应用层,时间的表达方式及精度各部相同。linux内核里面用一个名为jiffes的常量来计算时间戳。应用层有time、getdaytime等函数。

    在应用程序获取系统的启动时间,通过sysinfo中的uptime可以计算出系统的启动时间。

    2、sysinfo结构

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

     struct sysinfo {

                    long uptime;             /* Seconds since boot */

                    unsigned long loads[3];  /* 1, 5, and 15 minute load averages */

                    unsigned long totalram;  /* Total usable main memory size */

                    unsigned long freeram;   /* Available memory size */

                    unsigned long sharedram; /* Amount of shared memory */

                    unsigned long bufferram; /* Memory used by buffers */

                    unsigned long totalswap; /* Total swap space size */

                    unsigned long freeswap;  /* swap space still available */

                    unsigned short procs;    /* Number of current processes */

                    char _f[22];             /* Pads structure to 64 bytes */

                };

    3、获取系统启动时间

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

      #include <stdio.h>

      #include <sys/sysinfo.h>

      #include <time.h>

      #include <errno.h>

     

      static int print_system_boot_time()

      {

          struct sysinfo info;

          time_t cur_time = 0;

          time_t boot_time = 0;

          struct tm *ptm = NULL;

          if (sysinfo(&info)) {

          fprintf(stderr, "Failed to get sysinfo, errno:%u, reason:%s\n",

             errno, strerror(errno));

           return -1;

          }

         time(&cur_time);

         if (cur_time > info.uptime) {

          boot_time = cur_time - info.uptime;

          }

          else {

          boot_time = info.uptime - cur_time;

          }

          ptm = gmtime(&boot_time);

          printf("System boot time: %d-%-d-%d %d:%d:%d\n", ptm->tm_year + 1900,

          ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);

         return 0;

      }

     

      int main()

      {

          if (print_system_boot_time() != 0) {

          return -1;

          }

          return 0;

      }

    测试结果如下所:

     

    #查看系统安装时间

    [root@control1 ~]# tune2fs -l /dev/sda1 | grep create
    Filesystem created: Tue Aug 15 16:52:06 2017

    #通过查看系统创建账号时间来判断系统安装日期

    [root@zabbix-server ~]# passwd -S zabbix

    zabbix LK 2017-06-28 -1 -1 -1 -1 (密码已被锁定。)

     

     

     

    纵有白头俱老意,奈何缘浅路芊芊.
  • 相关阅读:
    前端 CSS 基础
    前端 HTML基础
    前端 JavaScript基础
    GoldenGate 复制进程报错"OGG-01296 Error mapping",丢弃文件报错“Mapping problem with delete record (target format)”,且实际条目存在
    SaltStack 与 Python 程序的结合
    SUSE-11 本地 zypper 配置
    Centos-7 + Docker-1.12 中 devicemapper + direct_lvm 的 Docker 存储配置
    Docker-v17 的层级(layer)概念
    Oracle-11g 中使用表空间透明数据加密(TDE)
    SaltStack 的远程执行机制
  • 原文地址:https://www.cnblogs.com/hanby/p/14148904.html
Copyright © 2011-2022 走看看