![](/img/trans.png)
[英]View output from a PHP script while it is running and log it to a file, and the script takes human input from STDIN
[英]Log output from bash script into log file and stdout while running from perl
我有perl脚本
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脚本(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
问题是,当调用install.pl
时,它会等待很长时间并且不提供正在安装的内容的任何输出信息。我需要它来更改perl脚本以显示bash脚本的输出信息(如果瞬时,则更好) ),以及将输出记录到日志文件中。 该脚本是由其他人编写的,我对perl脚本的了解很少。
你可以做类似的事情
open my $cmdOutut, "-|", "./ubuntu-lamp-install.sh";
while (<$cmdOutput>) {
print LOG "$_\n";
print "$_\n";
}
在您的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.