linux系统查看系统运行了多久,Linux下如何查看系统启动时间和运行时间安装时间...

您所在的位置:网站首页 怎样看电脑开机多久了 linux系统查看系统运行了多久,Linux下如何查看系统启动时间和运行时间安装时间...

linux系统查看系统运行了多久,Linux下如何查看系统启动时间和运行时间安装时间...

2024-01-14 05:20| 来源: 网络整理| 查看: 265

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

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

1:who命令查看

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

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

2: last  reboot

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

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

3:TOP命令查看

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

4: w命令查看

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

5:uptime 命令查看

6: 查看/proc/uptime

方法一:

方法二:

方法三:

1、前言

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

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

2、sysinfo结构

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

fz.gif

fz.gif

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 };

fz.gif

fz.gif

3、获取系统启动时间

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

fz.gif

fz.gif

1 #include

2 #include

3 #include

4 #include

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 }

fz.gif

fz.gif

测试结果如下所:

20190225090452731976.png

#查看系统安装时间

[[email protected] ~]# tune2fs -l /dev/sda1 | grep create

Filesystem created: Tue Aug 15 16:52:06 2017

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3