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秒

这样你就能轻松地应用Linux查看系统时间。

c代码:

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 */
unsigned long totalhigh; /* Total high memory size */
unsigned long freehigh; /* Available high memory size */
unsigned int mem_unit; /* Memory unit size in bytes */
char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */
};

  

#include <stdio.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <linux/unistd.h> /* for _syscallX macros/related stuff */
#include <linux/kernel.h> /* for struct sysinfo */
#include <sys/sysinfo.h> long get_uptime()
{
struct sysinfo s_info;
int error;
error = sysinfo(&s_info);
if(error != 0)
{
printf("code error = %d\n", error);
}
return s_info.uptime;
} long out_sys(){
/* Conversion constants. */
const long minute = 60;
const long hour = minute * 60;
const long day = hour * 24;
const double megabyte = 1024 * 1024;
/* Obtain system statistics. */
struct sysinfo si;
sysinfo (&si);
/* Summarize interesting values. */
printf ("system uptime : %ld days, %ld:%02ld:%02ld\n",
si.uptime / day, (si.uptime % day) / hour,
(si.uptime % hour) / minute, si.uptime % minute);
printf ("total RAM : %5.1f MB\n", si.totalram / megabyte);
printf ("free RAM : %5.1f MB\n", si.freeram / megabyte);
printf ("process count : %d\n", si.procs);
} int main(int argc, char* argv[]) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
printf("tv_sec=%llu tv_nsec=%llu\n uptime time:%llu\n",
(unsigned long long)t.tv_sec,
(unsigned long long)t.tv_nsec,
get_uptime()); out_sys();
return 0;
}

  

最新文章

  1. C#中使用Socket请求Web服务器过程
  2. js 求点到直线的距离(由2点确定的直线,求到第三点的距离)
  3. Effective Java 17 Design and document for inheritance or else prohibit it
  4. uva 10940
  5. Array类型(一)
  6. ios ColorLUT滤镜
  7. error C2664: “UINT GetDriveTypeW(LPCWSTR)”: 无法将参数 1 从“char [5]”转换为“LPCWSTR”
  8. poj2502最短路!
  9. ionic2新手入门整理,搭建环境,创建demo,打包apk,热更新,优化启动慢等避坑详解
  10. Velocity(4)——引入指令和#Parse 指令
  11. 详细:idea如何设置类头注释和方法注释
  12. 深入理解Java 注解原理
  13. Java 多线程 ReadWriteLock
  14. tmp32dll\sha1-586.asm(1432) : error A2070:invalid instruction operands 编译openssl出错
  15. Oracle EBS FA 资产取值
  16. 1032. Sharing (25)
  17. 2017-2018-2 20165327 实验三《敏捷开发与XP实践》实验报告
  18. logstash-input-jdbc同时同步多个表
  19. Log4Net从Mvc转到.Net Core
  20. PHP生成缩略图、加水印

热门文章

  1. Linux中2&gt;&amp;1使用
  2. 安装zkpython出错
  3. kvm虚拟化管理平台WebVirtMgr部署-完整记录(3)
  4. python升级后带来的几个小问题
  5. Python-注册登陆-20
  6. js控制css时注意
  7. Linux环境C程序设计
  8. 推广App篇
  9. Maven的课堂笔记2
  10. activiti-explorer disable demo