unbuffered output - Perl: Print log at runtime instead of dumping all at once later -
i have perl script runs jenkins slave. script executes shell script kept on remote box a. shell script deploys war on machine itself. both machines, jenkins slave , remote box centos instances.
use strict; use warnings; use cwd; use file::copy; use getopt::long; use file::basename; use net::openssh; ($conf_file, $environment, $doexec, $exec, $job, $dest_file, $user, $host, $ip, $target_server, $jenkins_job, $wrapper, $src_file, $src_path, $src_dist_path, $src_full_path, $id_file, $ssh, @array, $line); init(); sub init { $jenkins_job = $env{'job_name'}; $conf_file = "/home/ec2-user/scm/conf/deploy_build.conf"; open (fh, "<", $conf_file) or die "cannot open < $conf_file: $!"; while (<fh>) { if ( $_ =~ /\b$jenkins_job\b/ ) { push @array, $_; } else { next; } } foreach $line (@array) { ($job, $src_path, $dest_file, $user, $wrapper) = split(':', $line); $id_file = "/home/ec2-user/.ssh/priv_key"; $ssh = net::openssh->new($ip, key_path => $id_file, user => $user); $ssh->error , die "couldn't establish ssh connection: ". $ssh->error; printf "\n"; if (length $wrapper) { printf "initiating subroutine executing wrapper on remote machine...\n"; &exec_wrapper; } else { printf "*** no wrapper specified ****\n"; } } } sub exec_wrapper { ($stdout, $errput) = $ssh->capture2("~/release/$wrapper"); printf "output: $stdout\n" if $stdout; die "error: $errput\n" if $errput; printf "\n\n\n"; }
now issue though program runs fine, prints output after while. whole output captured in $stdout , dumped later. since job runs jenkins job, end user has no idea what's going on until output dumped. wanted print each line event occurs (runtime) instead of waiting whole output stored in variable , dumped later. did read this article i'm not sure whether applies case , if does, how can implement it. did try putting $| = 1;
@ top of script. didn't work. tried placing before initializing ssh object didn't work in of cases. mention, efficiency not concern in scenario.
any appreciated.
i got solution this node. instead of storing output in string , dumping later, can use following code print log real-time.
sub exec_wrapper { $ssh->system("~/release/$wrapper"); $ssh->die_on_error("execution of ~/release/$wrapper failed"); }
i'm open other approaches too.
Comments
Post a Comment