系统管理员的重要部分是监视和检查Linux系统已经走了很长时间。保持跟踪Linux系统的时间以优化系统资产是一个正确的概念。本文介绍了Tuptime工具,该工具 可以帮助系统管理员了解Linux计算机的启动和运行时间。
Tuptime工具用于报告Linux系统的历史和统计运行时间(正常运行时间),并在两次重新启动之间进行跟踪。
注册第一次启动时间
跟踪系统启动
计算系统关闭次数
计算正常运行时间和停机时间
计算最大,平均,低正常运行时间和停机时间
打印当前的正常运行时间
要安装Tuptime工具,应满足以下预安装要求
建立了Python 2.7或3.X,但建议使用最新版本。
Python模块(sys,os,optparse,sqlite3,datetime,locale,平台,子进程,时间)。
Linux操作系统
要在Linux上找到已安装的python,请使用以下命令-
$ apt-cache search python | egrep "^python2.[0-9] " --color
样本输出应如下所示–
python2.7 - Interactive high-level object-oriented language (version 2.7)
如果您的系统没有安装python,请使用以下命令安装python-
$ sudo apt-get install python2.7
样本输出应如下所示–
Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: python3.1-minimal Suggested packages: python3.1-doc python3.1-profiler The following NEW packages will be installed: python3.1 python3.1-minimal 0 upgraded, 2 newly installed, 0 to remove and 13 not upgraded. Need to get 5,444 kB of archives. After this operation, 19.9 MB of additional disk space will be used. Do you want to continue [Y/n]? y Get:1 http://debian.osuosl.org/debian/ squeeze/main python3.1-minimal amd64 3.1.3-1 [1,669 kB] Get:2 http://debian.osuosl.org/debian/ squeeze/main python3.1 amd64 3.1.3-1 [3,775 kB] Fetched 5,444 kB in 27s (201 kB/s) ...........................
要从git克隆Tuptime存储库,请使用以下命令–
$ cd /tmp $ git clone https://github.com/rfrail3/tuptime.git$ ls $ cd tuptime $ ls
样本输出应如下所示–
~$ cd /tmp /tmp$ git clone https://github.com/rfrail3/tuptime.gitCloning into 'tuptime'... remote: Counting objects: 2171, done. remote: Compressing objects: 100% (34/34), done. remote: Total 2171 (delta 21), reused 0 (delta 0), pack-reused 2137 Receiving objects: 100% (2171/2171), 749.33 KiB | 296.00 KiB/s, done. Resolving deltas: 100% (1056/1056), done. Checking connectivity... done. /tmp$ ls aptdaemon-A0RrYd sni-qt_vlc_15046-UFPBar unity_support_test.0 config-err-X4Efq6 sni-qt_vlc_15117-3D0jOa orbit-linux tuptime /tmp$ cd tuptime /tmp/tuptime$ ls CHANGELOG LICENSE scripts tuptime-install.sh debian README.md src tuptime-manual.txt
如下所示安装tuptime-install.sh –
# chmod +x tuptime-install.sh # ./tuptime-install.sh
样本输出应如下所示–
Tuptime installation script Clonning repository... Cloning into '/tmp/tmp.P6j2npOJVU'... remote: Counting objects: 2171, done. remote: Compressing objects: 100% (34/34), done. remote: Total 2171 (delta 21), reused 0 (delta 0), pack-reused 2137 Receiving objects: 100% (2171/2171), 749.33 KiB | 318.00 KiB/s, done. Resolving deltas: 100% (1056/1056), done. Checking connectivity... done. Copying files... Creating tuptime user... Creating tuptime db Setting tuptime db ownership Executing tuptime with tuptime user for testing Copying init debian file... update-rc.d: warning: default start runlevel arguments (2 3 4 5) do not match tuptime Default-Start values (1 2 3 4 5) update-rc.d: warning: default stop runlevel arguments (0 1 6) do not match tuptime Default-Stop values (0 6) ....................................
要运行Tuptime,请使用以下命令-
# tuptime
样本输出应如下所示–
System startups: 1 since 12:16:47 Tuesday 08 March 2016 System shutdowns: 0 ok - 0 bad System uptime: 100.0 % - 1 day, 2 hours, 18 minutes and 8 seconds System downtime: 0.0 % - 0 seconds System life: 1 day, 2 hours, 18 minutes and 8 seconds Largest uptime: 1 day, 2 hours, 18 minutes and 8 seconds from 12:16:47 Tuesday 08 March 2016 Shortest uptime: 1 day, 2 hours, 18 minutes and 8 seconds from 12:16:47 Tuesday 08 March 2016 Average uptime: 1 day, 2 hours, 18 minutes and 8 seconds Largest downtime: 0 seconds Shortest downtime: 0 seconds Average downtime: 0 seconds Current uptime: 1 day, 2 hours, 18 minutes and 8 seconds since 12:16:47 Tuesday 08 March 2016
要显示沿日期和时间的输出,请使用以下命令–
# tuptime --date='%H:%M:%S %d-%m-%Y'
样本输出应如下所示–
System startups: 1 since 12:16:47 08-03-2016 System shutdowns: 0 ok - 0 bad System uptime: 100.0 % - 1 day, 2 hours, 19 minutes and 41 seconds System downtime: 0.0 % - 0 seconds System life: 1 day, 2 hours, 19 minutes and 41 seconds Largest uptime: 1 day, 2 hours, 19 minutes and 41 seconds from 12:16:47 08-03-2016 Shortest uptime: 1 day, 2 hours, 19 minutes and 41 seconds from 12:16:47 08-03-2016 Average uptime: 1 day, 2 hours, 19 minutes and 41 seconds Largest downtime: 0 seconds Shortest downtime: 0 seconds Average downtime: 0 seconds Current uptime: 1 day, 2 hours, 19 minutes and 41 seconds since 12:16:47 08-03-2016
要将系统文件打印为列表,请使用以下命令–
# tuptime --list
样本输出应如下所示–
Startup: 1 at 12:16:47 Tuesday 08 March 2016 Uptime: 1 day, 2 hours, 21 minutes and 8 seconds
要创建一个备用数据库文件来存储Tuptime日志,如下所示–
# tuptime --filedb /tmp/tuptime_testdb.db
样本输出应如下所示–
System startups: 1 since 12:16:47 Tuesday 08 March 2016 System shutdowns: 0 ok - 0 bad System uptime: 100.0 % - 1 day, 2 hours, 22 minutes and 36 seconds System downtime: 0.0 % - 0 seconds System life: 1 day, 2 hours, 22 minutes and 36 seconds Largest uptime: 1 day, 2 hours, 22 minutes and 36 seconds from 12:16:47 Tuesday 08 March 2016 Shortest uptime: 1 day, 2 hours, 22 minutes and 36 seconds from 12:16:47 Tuesday 08 March 2016 Average uptime: 1 day, 2 hours, 22 minutes and 36 seconds Largest downtime: 0 seconds Shortest downtime: 0 seconds Average downtime: 0 seconds Current uptime: 1 day, 2 hours, 22 minutes and 36 seconds since 12:16:47 Tuesday 08 March 2016
要获取有关在结束状态下关闭电源的信息,请使用以下命令–
# tuptime --end --table
样本输出应如下所示–
No. Startup Date Uptime Shutdown Date End Downtime 1 12:16:47 Tuesday 08 March 2016 1 day, 2 hours, 23 minutes and 42 seconds
要打印内核信息,请使用以下命令–
# tuptime --kernel
样本输出应如下所示–
System startups: 1 since 12:16:47 Tuesday 08 March 2016 System shutdowns: 0 ok - 0 bad System uptime: 100.0 % - 1 day, 2 hours, 25 minutes and 4 seconds System downtime: 0.0 % - 0 seconds System life: 1 day, 2 hours, 25 minutes and 4 seconds System kernels: 1 Largest uptime: 1 day, 2 hours, 25 minutes and 4 seconds from 12:16:47 Tuesday 08 March 2016 ...with kernel: Linux-4.2.0-30-generic-x86_64-with-Ubuntu-14.04-trusty Shortest uptime: 1 day, 2 hours, 25 minutes and 4 seconds from 12:16:47 Tuesday 08 March 2016 ...with kernel: Linux-4.2.0-30-generic-x86_64-with-Ubuntu-14.04-trusty Average uptime: 1 day, 2 hours, 25 minutes and 4 seconds Largest downtime: 0 seconds ...with kernel: None Shortest downtime: 0 seconds ...with kernel: None Average downtime: 0 seconds Current uptime: 1 day, 2 hours, 25 minutes and 4 seconds since 12:16:47 Tuesday 08 March 2016 ...with kernel: Linux-4.2.0-30-generic-x86_64-with-Ubuntu-14.04-trusty
要获取Tuptime 帮助信息,请使用以下命令-
# tuptime --help
标准输出应如下所示–
Usage: tuptime [options] Options: -h, --help show this help message and exit -c, --ckernel classify / order by kernel -d DATE_FORMAT, --date=DATE_FORMAT date format output -e, --end order by end state -f FILE, --filedb=FILE database file -g, --graceful register a gracefully shutdown -k, --kernel print kernel information -l, --list enumerate system life as list -o, --offtime order by offtime / downtime -r, --reverse reverse order -s, --seconds output time in seconds and epoch -t, --table enumerate system life as table -u, --uptime order by uptime -v, --verbose verbose output -V, --version show version -x, --silent update values into db without output
恭喜你!现在,您知道“如何显示Linux系统的历史和统计运行时间”。在下一篇Linux文章中,我们将学习有关这些命令类型的更多信息。继续阅读!