简体   繁体   English

从perl运行时,将bash脚本的输出记录到日志文件和stdout中

[英]Log output from bash script into log file and stdout while running from perl

I have perl script 我有perl脚本

perl script (install.pl): perl脚本(install.pl):

use strict; use warnings;
use Term::ANSIColor;
my $logfilename = "install.log";
open LOG,">$logfilename" or die "failed to open log file reason:$!";
sub logMsg
{
    my ($msg) = @_;
    my ($error) = $_[1];
    $msg .= "\n";
    print LOG $msg;
    if( $error ) { print color 'red' ;}
    {print $msg}
    if( $error ) { print color 'reset' ;}   
}
sub lampInstall
{
    logMsg("Installing apache2, php, and mysql db");
    # Install apache2 and mysql:
    my $cmdOutput = `./ubuntu-lamp-install.sh`;
    print LOG "$cmdOutput\n";
}
lampInstall();
close LOG;

bash script (ubuntu-lamp-install.sh) bash脚本(ubuntu-lamp-install.sh)

#!/bin/bash
sudo apt-get update
sudo apt-get install ntp
sudo /etc/init.d/ntp start
export DEBIAN_FRONTEND=noninteractive
echo "mysql-server mysql-server/root_password password test" | sudo debconf-set-selections
echo "mysql-server mysql-server/root_password_again password test" | sudo debconf-set-selections
sudo apt-get install -y apache2 php5 libapache2-mod-php5
sudo service apache2 restart
sudo a2enmod ssl

The issues is that , when install.pl is invoked, it waits for long time and does not give any output information of what is being installed.I need it to change the perl script to display the output information from bash script(better if instantaneous) as well as log the output to log file. 问题是,当调用install.pl时,它会等待很长时间并且不提供正在安装的内容的任何输出信息。我需要它来更改perl脚本以显示bash脚本的输出信息(如果瞬时,则更好) ),以及将输出记录到日志文件中。 The script is written by someone else , I have very little knowledge of perl scripting. 该脚本是由其他人编写的,我对perl脚本的了解很少。

You could do something like 你可以做类似的事情

open my $cmdOutut, "-|", "./ubuntu-lamp-install.sh";
while (<$cmdOutput>) {
    print LOG "$_\n";
    print "$_\n";
}

in your lampInstall() . 在您的lampInstall()

This is the new lampInstall() : 这是新的lampInstall()

sub lampInstall
{
    logMsg("Installing apache2, php, and mysql db");
    # Install apache2 and mysql:
    open my $cmdOutut, "-|", "./ubuntu-lamp-install.sh";
    while (<$cmdOutput>) {
        print LOG "$_\n";
        print "$_\n";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM