大家好,我是梁旭。作为一名合格的Linux运维人员,监控系统的正常运行时间可以说是一项基本功。今天的文章将与大家分享一个实用的命令行工具,用于记录和跟踪系统的运行时间。它的名字是正常运行的。之所以推荐uptimed,是因为它不仅可以记录和跟踪系统的运行时间,还可以直接将这些历史记录以摘要的形式展示给用户,比如你的系统运行了多久?最长运行时间记录是多少?系统什么时候重启的?等等,它也更容易上手。下面简单介绍一下uptimed的安装和使用。安装uptimed工具主流Linux发行版的官方仓库中已经提供了uptimed。在ArchLinux上安装:$sudopacman-Suptimed在Debian、Ubuntu、LinuxMint上安装:$sudoapt-getinstalluptimed在Ubuntu及其衍生版上安装之前,请确保添加universe存储库,如果没有,请运行以下命令添加:$sudoadd-apt-repositoryuniverse在Fedora上安装:$sudodninstalluptimed在CentOS7上安装:由于CentOS7默认仓库中没有uptimed,需要先添加EPEL仓库:$sudoyuminstalllepel-release然后运行以下命令安装:$sudoyuminstalluptimedmanually安装:对于其他linux系统,或者上述方法安装失败,可以到分发页面下载源码,然后手动编译安装:#wgethttps://github.com/rpodgorny/uptimed/archive/v0.4.2.zip-及时更新。zip#unzipuptimed.zip#cduptimed-0.4.2/#./configure#make#makeinstall安装完成后,启用并启动服务:#systemctlenableuptimed#systemctlstartuptimed查看服务的启动状态:#systemctlstatusuptimeduptimed.service-UptimerecordtrackingdaemonLoaded:loaded(/usr/lib/systemd/system/uptimed.service;disabled;vendorpreset:disabled)Active:active(running)sinceTue2020-02-1114:21:12IST;1sagoDocs:man:uptimed(8)man:uprecords(1)MainPID:1435(uptimed)Status:"Nextmilestone(fivedays)atSunFeb1614:13:592020"CGroup:/system.slice/uptimed.service└─1435/usr/sbin/uptimed-fFeb1114:21:12server.ostechnix.localsystemd[1]:StartingUptimerecordtrackingdaemon...Feb1114:21:12server.ostechnix.localsystemd[1]:StartedUptimerecordtrackingdaemon。这样,前面的安装工作就完成了,使用uptimed工具uptimed工具安装完成后,运行uprecords命令显示系统运行时间的历史摘要:#uprecords输出摘要结果:#Uptime|SystemBootup---------------------------+--------------------------------------------------10天,02:35:51|Linux3.10.0-1062.1.1.elTueFeb1114:22:33202020天,00:07:27|Linux3.10.0-1062.1.1.elTueFeb1114:13:592020->30天,00:02:11|Linux3.10.0-1062.1.1.elTueFeb1117:02:292020-----------------------------+--------------------------------------------------1upin0days,00:05:17|atTueFeb1117:09:562020no1in0days,02:33:41|atTueFeb1119:38:202020up0days,02:45:29|sinceTueFeb1114:13:592020down0days,00:05:12|sinceTueFeb1114:13:592020%up96.953|sinceTueFeb1114:13:592020这里的->符号表示已经定位到系统的最晚启动时间。使用-b按开机时间正序输出:#uprecords-b#Uptime|SystemBootup----------------------------+-----------------------------------------------10天,00:07:27|Linux3.10.0-1062.1.1.elTueFeb1114:13:59202020天,02:35:51|Linux3.10.0-1062.1.1.elTueFeb1114:22:332020->30天,00:30:14|Linux3.10.0-1062.1.1.elTueFeb1117:02:292020使用-B开机时间倒序输出:#uprecords-B#Uptime|SystemBootup-----------------------------+----------------------------------------------------->10天,00:14:51|Linux3.10.0-1062.1.1.elTueFeb1117:02:29202020天,02:35:51|Linux3.10.0-1062.1。1.elTueFeb1114:22:33202030days,00:07:27|Linux3.10.0-1062.1.1.elTueFeb1114:13:592020使用-m自定义输出记录条数:不加参数则输出第一个默认10条记录,要输出前20条记录,请运行:#uprecords-m20使用-w输出完整信息:以上结果中Linux的内核信息是部分的省略,如果要输出完整的内核信息,请使用-w进行宽输出:#uprecords-w#Uptime|SystemBootup-----------------------------+------------------------------------------------10天,02:35:51|Linux3.10.0-1062.1.1.el7.x86_64TueFeb1114:22:332020->20天,00:16:35|Linux3.10.0-1062.1.1.el7.x86_64TueFeb1117:02:29202030days,00:07:27|Linux3.10.0-1062.1.1.el7.x86_64TueFeb1114:13:592020---------------------------+-------------------------------------------------no1in0days,02:19:17|atTueFeb1119:38:212020up0days,02:59:53|sinceTueFeb1114:13:592020down0days,00:05:12|sinceTueFeb1114:13:592020%up97.190|sinceTueFeb1114:13:592020使用-d显示关机时间:-d可以将上面的内核信息替换为关机时间信息:#uprecords-d#Uptime|LastdowntimeBootup--------------------------+------------------------------------------------10天,02:35:51|0天,00:01:07TueFeb1114:22:332020->20天,00:13:02|0天,00:04:05TueFeb1117:02:29202030days,00:07:27|0days,00:00:00TueFeb1114:13:592020----------------------------+--------------------------------------------------no1in0days,02:22:50|atTueFeb1119:38:212020up0days,02:56:20|sinceTueFeb1114:13:592020down0days,00:05:12|sinceTueFeb1114:13:592020%up97。136|sinceTueFeb1114:13:592020查看更多命令选项:#uprecords-?usage:uprecords[OPTION]...-?thishelp-adonotprintansicodes-bsortbyboottime-Breversesortbyboottime-ksortbysysinfo-Kreversesortbysysinfo-dprintdowntimeseenbeforeeveryuptimesinsteadofsystem-cdonotshowcurrententryifnotintopentries-frunco??ntinouslyinaloop-sdonotprintextrastatistics-wwideoutput(morethan80colsperline)-iINTERVALuseINTERVALsecondsforloopinsteadof5,implies-f-mCOUNTshowamaximumoftopCOUNTentriesinsteadof10-Mshownextmilestone-vversioninformation好了,这次的分享就到这里了,上面所给出的示例在日常使用中非常常见。如果想了解uptimed的更多用法,可以参考它的帮助手册$manuprecords。希望本文对您有所帮助!
