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即可算出系统启动时间
- 代码: 全选
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 查看当前系统运行时间
[root@DB-Server ~]# who -b
system boot May 11 09:27
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
如果只需要查看最后一次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后表示系统到目前运行了多久时间。反过来推算系统重启时间
4:w 命令查看
4: w命令查看
如下截图所示,up后表示系统到目前运行了多久时间。反过来推算系统重启时间

5:uptime 命令查看
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 }


测试结果如下所:
Linux下如何查看系统启动时间和运行时间的更多相关文章
- Linux下如何查看系统启动时间和运行时间以及安装时间
1.uptime命令输出:16:11:40 up 59 days, 4:21, 2 users, load average: 0.00, 0.01, 0.00 2.查看/proc/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文件计算 ...
- [转载]linux下如何查看系统和内核版本
原文地址:linux下如何查看系统和内核版本作者:vleage 1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2 ...
- 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文件计算 ...
- linux 下如何查看和踢除正在登陆的其它用户 ==>Linux下用于查看系统当前登录用户信息的4种方法
在linux系统中用pkill命令踢出在线登录用户 由于linux服务器允许多用户登录,公司很多人知道密码,工作造成一定的障碍 所以需要有时踢出指定的用户 1/#who 查出当前有那些终端登录(用 ...
- Linux下用于查看系统当前登录用户信息 w命令
作为系统管理员,你可能经常会(在某个时候)需要查看系统中有哪些用户正在活动.有些时候,你甚至需要知道他(她)们正在做什么.本文为我们总结了4种查看系统用户信息(通过编号(ID))的方法. 1. 使用w ...
- Linux下用于查看系统当前登录用户信息的4种方法
1. 使用w命令查看登录用户正在使用的进程信息 w命令用于显示已经登录系统的用户的名称,以及他们正在做的事.该命令所使用的信息来源于/var/run/utmp文件.w命令输出的信息包括: 用户名称 用 ...
- linux下如何查看系统和内核版本
1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@cro ...
- Linux下如何查看系统是多少位的
在Linux命令行下输入 getconf LONG_BIT 命令
随机推荐
- C#读取Excel遇到无法读取的解决方法
C#读取Excel遇到无法读取的解决方法:1.在导入数据连接字符串中,将IMEX=1加入,“Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\ ...
- java comet
http://www.javaworld.com/article/2077995/java-concurrency/asynchronous-processing-support-in-servlet ...
- c# 获取某日期所在周的第一天和最后一天
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WyfC ...
- Android开发:程序目录结构详解
HelloWorld程序的目录结构概述 我们可以在文件夹中看到,HelloWorld程序的目录主要包括:src文件夹.gen文件夹.Android文件夹.assets.res文件夹. AndroidM ...
- jQuery中时间戳和日期的相互转换
在项目中经常会使用时间戳和日期的相互转换,可以参考如下代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 ...
- openstack changePassword
http://niusmallnan.github.io/_build/html/ 在创建虚拟机时候,通常我们需要让用户填写虚机系统的初始化密码,因为很多人并不习惯使用秘钥方式ssh登录, 设置其用户 ...
- CPU阿甘:函数调用的秘密
个人感言:真正的知识是深入浅出的,码农翻身" 公共号将苦涩难懂的计算机知识,用形象有趣的生活中实例呈现给我们,让我们更好地理解.感谢"码农翻身" 公共号,感谢你们的成果, ...
- C# 时间函数相减
1:第一种方式: "; "; DateTime firstDateTemp = DateTime.ParseExact(firsttime, "yyyyMMddHHmms ...
- Foundation ----->NSSet
1.集合类 NSString *s1 = @"zhangsan"; NSString *s2 = @"lisi"; NSString * ...
- Sql With as 用法
with district as ( select * from Area where AbbrTW= N'中國' union all select a.* from Ar ...