#!/usr/bin/env perl
# weather_watch.pl
use strict;
use Getopt::Long;
use JSON::XS;
use LWP::Simple;
use Sys::Syslog;
my $app = bless {
city => 'London,uk',
};
GetOptions(
$app,
'city=s',
);
$app->init;
$app->run;
sub init {
my ($app) = @_;
openlog($0, 'pid', 'user');
syslog("info", "Starting up");
$SIG{TERM} = sub {
$app->{should_stop} = 1;
};
}
sub run {
my ($app) = @_;
my $url = "http://api.openweathermap.org/data/2.5/weather?q=$app->{city}";
until ( $app->{should_stop} ) {
if ( my $json = get($url) ) {
my $data = decode_json($json);
syslog("info", "Temperature is $data->{main}{temp}");
syslog("info", "Wind speed is $data->{wind}{speed}");
}
sleep 5;
}
syslog("info", "Shutting down");
closelog();
}
####
#!/usr/bin/perl
# weathermon
use warnings;
use strict;
use Daemon::Control;
use Getopt::Long;
GetOptions(
\ my %OPT,
'city=s',
);
exit Daemon::Control->new(
name => "Weather watch daemon",
path => '/home/arun/test/weathermon',
program => '/home/arun/test/weather_watch.pl',
program_args => [ '--city', $OPT{city} ],
pid_file => '/tmp/weathermon.pid',
)->run;
##
##
% ./weathermon status
Weather watch daemon [Not Running]
% ./weathermon start
Weather watch daemon [Started]
% ./weathermon status
Weather watch daemon [Running]
% ./weathermon stop
Weather watch daemon [Stopped]
% ./weathermon start -c Miami,us
Weather watch daemon [Started]
% ./weathermon restart
Weather watch daemon [Stopped]
Weather watch daemon [Started]
%