【发布时间】:2017-08-03 20:32:31
【问题描述】:
我有一个 Ubuntu 16.04.1 服务器,我乱用它。目前我正在使用它来监控我的互联网速度和连接。
我已经编写了一个脚本并使用 Perl 和 crontab 将其自动化,但现在我想清理我的日志文件。
以下是执行 Perl 脚本时写入日志文件的内容:
Time: 12:40:01
Retrieving speedtest.net configuration...
Testing from TWC (now Spectrum) (123.45.67.890)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Fibrant (Salisbury, NC) [60.95 km]: 51.184 ms
Testing download speed................................................................................
Download: 37.76 Mbit/s
Testing upload speed....................................................................................................
Upload: 11.56 Mbit/s
我希望得到以下输出:
Download: 37.76 Mbit/s
Upload: 11.56 Mbit/s
我当前的 Perl 脚本如下所示:
use strict;
use warnings;
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime( time );
my $nice_timestamp = sprintf( "%04d%02d%02d", $year + 1900, $mon + 1, $mday );
my $timer = sprintf( "%02d:%02d:%02d", $hour, $min, $sec );
my $filename = "/home/user/DailyLogs/InternetLog$nice_timestamp";
open( my $fh, '>>', $filename ) or die "Could not open file '$filename' $!";
my $output = qx"/home/user/.local/bin/speedtest-cli";
print $fh "Time: ";
print $fh $timer;
print $fh "\n";
print $fh $output;
print $fh "\n\n";
close $fh;
任何帮助将不胜感激。谢谢!
【问题讨论】:
标签: perl command-line scripting