简体   繁体   English

从cron运行时,Perl脚本不会将STDOUT输出到文件

[英]Perl script does not output STDOUT to file when run from cron

Fairly new to Perl. 对Perl来说还很陌生。 I have a Perl script on a Linux machine, which has own logfile. 我在Linux机器上有一个Perl脚本,它具有自己的日志文件。 Logfile name can change, dependent on data the script is working on (date, filename, datatype, etc.) 日志文件名称可以更改,具体取决于脚本正在处理的数据(日期,文件名,数据类型等)。

The script at some pionts is calling a native executable with system() call, which gives some information out to STDOUT and STDERR - few tens to few hundreds lines over many minutes. 某些情况下,脚本正在通过system()调用来调用本机可执行文件,这会将一些信息提供给STDOUT和STDERR-在几分钟内几十到几百行。 After the executable is done, the script continues and logs some other info to the logfile. 可执行文件完成后,脚本将继续并将其他一些信息记录到日志文件中。

Until now the script only logged its own output, without the native executables output, which I want to log in same files as the Perl script logs to. 到目前为止,该脚本仅记录了自己的输出,而没有本机可执行文件的输出,我想将其与Perl脚本登录的文件记录在同一文件中。 Tried it with following two methods: 使用以下两种方法进行了尝试:

#!/usr/bin/perl
#some other code
@array_executable_and_parameters = qw/echo foo/ ;
open $log_fh, '>>', 'log/logfile1.txt';
*STDOUT = $log_fh;
*STDERR = $log_fh;
print "log_fh=$log_fh\n";
system( @array_executable_and_parameters);

$logfilename='log/logfile2.txt';
open(LOGFILEHANDLE, ">>$logfilename" );
*STDOUT = LOGFILEHANDLE;
*STDERR = LOGFILEHANDLE;
print LOGFILEHANDLE "Somethinglogged\n";
system( @array_executable_and_parameters);

It works when I run the script manually, but not when run from cron. 当我手动运行脚本时有效,但从cron运行时则无效。

I know it is possible to redirect in the crontab by Linux means, but then I have to know the filename to log to, which only will be known when some data arrives, so seems to me not feasible. 我知道可以通过Linux方式在crontab中进行重定向,但是随后我必须知道要登录的文件名,只有在某些数据到达时才能知道该文件名,因此在我看来不可行。 I also would like to have as much as possible inside the script, without many dependencies on the Linux etc. I have also no possibility to install any extra modules, libraries for Perl to use, suppose it is bare minimum install. 我还希望脚本中尽可能多的内容,而对Linux等没有太多依赖性。我也没有可能安装任何额外的模块,供Perl使用的库,假设这只是最低限度的安装。

How do I get STDOUT and STDERR redirected to a specific file from inside the Perl script? 如何从Perl脚本中将STDOUT和STDERR重定向到特定文件?

And if possible, how do I detect filename the STDOUT currently goes to? 如果可能,如何检测STDOUT当前使用的文件名?

Reassigning *STDOUT is only affecting the Perl-internal STDOUT scalar's binding. 重新分配*STDOUT仅影响Perl内部STDOUT标量的绑定。 The proper way to redirect standard output on the system level is something like 在系统级别重定向标准输出的正确方法是

open (STDOUT, '>&', $log_fh) or die "$0: could not: $!";

You should similarly report errors from your other system calls which could fail (and use strict and etc). 同样,您应该从其他系统调用中报告可能失败的错误(并use strict和其他)。

cron runs your job in your home directory, so if the path $HOME/log does not exist, the script will fail to open the log file handle (silently, because you are not logging open errors!) cron在主目录中运行您的作业,因此,如果路径$HOME/log不存在,该脚本将无法打开日志文件句柄(静默,因为您没有记录open错误!)

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

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