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

代码: 
  1. [root@localhost ~]# date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"
  2. 2017-10-27 14:32:35

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

代码: 
  1. [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)}'
  2. 系统已运行:002434

1: who 命令查看

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

  1. [root@localhost ~]# who -b
  2. system boot 2017-10-27 14:32

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

  1. [root@localhost ~]# who -r
  2. run-level 3 2017-10-27 14:33

2: last  reboot

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

[root@DB-Server ~]# last reboot

  1. [root@localhost ~]# last reboot
  2. reboot system boot 3.10.0-327.el7.x Fri Oct 27 14:32 - 14:59 (00:26)
  3.  
  4. wtmp begins Fri Oct 27 14:32:39 2017

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

  1. [root@DB-Server ~]# last reboot | head -1
  2. reboot
  3. system boot 2.6.9-42.ELsmp Thu May 29 15:25 (00:08)

3:TOP命令查看

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

  1. [root@localhost ~]# who -b
  2. system boot 2017-10-27 14:32
  3. top - 15:00:29 up 27 min, 2 users, load average: 0.00, 0.01, 0.05
  4. Tasks: 99 total, 1 running, 98 sleeping, 0 stopped, 0 zombie
  5. %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
  6. KiB Mem : 8176008 total, 7892856 free, 131764 used, 151388 buff/cache
  7. KiB Swap: 8257532 total, 8257532 free, 0 used. 7868548 avail Mem
  8.  
  9. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  10. 1855 root 20 0 0 0 0 S 0.3 0.0 0:00.33 kworker/0:2
  11. 1 root 20 0 44496 7168 2612 S 0.0 0.1 0:02.32 systemd
  12. 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
  13. 3 root 20 0 0 0 0 S 0.0 0.0 0:00.05 ksoftirqd/0
  14. 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
  15. 6 root 20 0 0 0 0 S 0.0 0.0 0:00.03 kworker/u4:0
  16. 7 root rt 0 0 0 0 S 0.0 0.0 0:00.06 migration/0
  17. 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
  18. 9 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/0
  19. 10 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcuob/1
  20. 11 root 20 0 0 0 0 S 0.0 0.0 0:00.33 rcu_sched
  21. 12 root 20 0 0 0 0 S 0.0 0.0 0:00.12 rcuos/0
  22. 13 root 20 0 0 0 0 S 0.0 0.0 0:00.37 rcuos/1
  23. 14 root rt 0 0 0 0 S 0.0 0.0 0:00.01 watchdog/0
  24. 15 root rt 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/1
  25. 16 root rt 0 0 0 0 S 0.0 0.0 0:00.08 migration/1
  26. 17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/1
  27. 19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1:0H

4: w命令查看

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

  1. [root@localhost ~]# w
  2. 15:00:56 up 28 min, 2 users, load average: 0.00, 0.01, 0.05
  3. USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
  4. root tty1 14:33 25:52 0.07s 0.07s -bash
  5. root pts/0 10.0.100.55 14:37 0.00s 0.10s 0.00s w

5:uptime 命令查看

  1. [root@localhost ~]# uptime
  2. 15:01:40 up 29 min, 2 users, load average: 0.00, 0.01, 0.05

6: 查看/proc/uptime

方法一:

  1. [root@localhost ~]# cat /proc/uptime
  2. 1778.05 3520.28

方法二:

  1. [root@localhost ~]# date -d "`cut -f1 -d. /proc/uptime` seconds ago"
  2. Fri Oct 27 14:32:35 CST 2017

 方法三:

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

1、前言

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

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

2、sysinfo结构

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

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

3、获取系统启动时间

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

 
  1. 1 #include <stdio.h>
  2. 2 #include <sys/sysinfo.h>
  3. 3 #include <time.h>
  4. 4 #include <errno.h>
  5. 5
  6. 6 static int print_system_boot_time()
  7. 7 {
  8. 8 struct sysinfo info;
  9. 9 time_t cur_time = 0;
  10. 10 time_t boot_time = 0;
  11. 11 struct tm *ptm = NULL;
  12. 12 if (sysinfo(&info)) {
  13. 13 fprintf(stderr, "Failed to get sysinfo, errno:%u, reason:%s\n",
  14. 14 errno, strerror(errno));
  15. 15 return -1;
  16. 16 }
  17. 17 time(&cur_time);
  18. 18 if (cur_time > info.uptime) {
  19. 19 boot_time = cur_time - info.uptime;
  20. 20 }
  21. 21 else {
  22. 22 boot_time = info.uptime - cur_time;
  23. 23 }
  24. 24 ptm = gmtime(&boot_time);
  25. 25 printf("System boot time: %d-%-d-%d %d:%d:%d\n", ptm->tm_year + 1900,
  26. 26 ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
  27. 27 return 0;
  28. 28 }
  29. 29
  30. 30 int main()
  31. 31 {
  32. 32 if (print_system_boot_time() != 0) {
  33. 33 return -1;
  34. 34 }
  35. 35 return 0;
  36. 36 }

测试结果如下所:

#查看系统安装时间

[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 (密码已被锁定。)

Linux下如何查看系统启动时间和运行时间以及安装时间的更多相关文章

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

    1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.002.查看/proc/uptime文件计算系 ...

  2. Linux下如何查看系统启动时间和运行时间(转)

    1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.00 2.查看/proc/uptime文件计算 ...

  3. [转载]linux下如何查看系统和内核版本

    原文地址:linux下如何查看系统和内核版本作者:vleage 1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2 ...

  4. uptime命令查看系统启动时间和运行时间、查看linux系统负载

    1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.00 2.查看/proc/uptime文件计算 ...

  5. linux 下如何查看和踢除正在登陆的其它用户 ==>Linux下用于查看系统当前登录用户信息的4种方法

    在linux系统中用pkill命令踢出在线登录用户 由于linux服务器允许多用户登录,公司很多人知道密码,工作造成一定的障碍 所以需要有时踢出指定的用户 1/#who   查出当前有那些终端登录(用 ...

  6. Linux下用于查看系统当前登录用户信息 w命令

    作为系统管理员,你可能经常会(在某个时候)需要查看系统中有哪些用户正在活动.有些时候,你甚至需要知道他(她)们正在做什么.本文为我们总结了4种查看系统用户信息(通过编号(ID))的方法. 1. 使用w ...

  7. Linux下用于查看系统当前登录用户信息的4种方法

    1. 使用w命令查看登录用户正在使用的进程信息 w命令用于显示已经登录系统的用户的名称,以及他们正在做的事.该命令所使用的信息来源于/var/run/utmp文件.w命令输出的信息包括: 用户名称 用 ...

  8. linux下如何查看系统和内核版本

        1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@cro ...

  9. Linux下如何查看系统是多少位的

    在Linux命令行下输入  getconf LONG_BIT 命令

随机推荐

  1. 在Django中使用ORM创建图书管理系统

    一.ORM(对象关系映射) 很多语言的web框架中都有这个概念 1. 为什么要有ORM? 1. 写程序离不开数据,要使用数据就需要连接数据库,但是不同的数据库在sql语句上(mysql,oracle等 ...

  2. 如何打印consul的错误信息

    在配置文件中添加 management: endpoints: web: exposure: include: "*" endpoint: shutdown: enabled: t ...

  3. Yii2.0 安装使用报错:yii\web\Request::cookieValidationKey must be configured with a secret key.

    下载了Yii2.0的basic版,配置好apache之后,浏览器访问,出现如下错误: Invalid Configuration – yii\base\InvalidConfigException y ...

  4. web.xml:<url-pattern>

    web.xml 中的 <url-pattern> 是 <servlet-mapping> 或 <filter-mapping> 下的子标签. url :http:/ ...

  5. 金融量化分析【day110】:NumPy通用函数

    一.通用函数 能同时对数组中所有元素进行运算的函数 1.一元函数 1.sqrt 2.ceil 3.modf 4.isnan 5.abs 2.二元函数 1.maxinum 二.数学和统计方法 1.sum ...

  6. React 记录(4)

    React文档:https://www.reactjscn.com/docs/components-and-props.html 慢慢学习:对照教程文档,逐句猜解,截图 React官网:https:/ ...

  7. Docker下安装Jenkins

    Docker安装参见:https://www.cnblogs.com/hackyo/p/9280042.html 安装Jenkins: docker run \ -u root \ --rm \ -d ...

  8. velocity 新手用小常识--开源,简单易上手

    项目中经常用到的 .vm 后缀文件是什么? 基于 java 的 velocity 模版引擎的一种页面控制文件,是一些类似 html 语句和一种叫 VLT 的语句构成   velocity --美 [v ...

  9. [物理学与PDEs]第3章习题7 快、慢及Alfv\'en 特征速度的比较

    证明: 当 $H_1\neq 0$ 及 $H_2^2+H_3^2\neq 0$ 时, 快.慢及 Alfv\'en 特征速度 $C_f$, $C_s$ 及 $C_a$ 满足 $$\bex 0<C_ ...

  10. Executors的四种线程池

    Executors.newCachedThreadPool(); Executors.newFixedThreadPool(2); Executors.newScheduledThreadPool(2 ...