简体   繁体   English

Linux命令(cp,rm)不在perl脚本中执行。 一些作品。 但是没有错误返回

[英]Linux commands (cp, rm) are not executed in a perl script. Some works. But no error are returned

I'm having a very strange error. 我有一个非常奇怪的错误。

I run a perl script which executes linux commands. 我运行一个执行Linux命令的perl脚本。 They are executed like this: 它们是这样执行的:

my $err = `cp -r $HTML /tssobe/www/tstweb/$subpath/$HTMLDIR1`;
myLog("$err");

And $err is empty, which mean the command didn't return and error. $ err为空,这意味着该命令未返回且出错。 (right?) (对?)

I tried to execute the linux command with exec "" or system (), but no success. 我试图用exec“”或system()执行linux命令,但是没有成功。 I tried to change the path. 我试图改变道路。 Same. 相同。

Also, I tried to run only the cp command in a new perl script. 另外,我尝试在新的perl脚本中仅运行cp命令。 It works. 有用。 But not in my full perl script. 但不是在我完整的perl脚本中。

In this perl script, some commands are working, some are not. 在此perl脚本中,某些命令有效,有些则无效。

The script was working yesterday, Not anymore this morning. 该脚本昨天有效,今天上午不再可用。 No changes have been made in the meantime. 在此期间,未进行任何更改。

I tried a lot of things, I would be glad if anybody has an idea. 我尝试了很多事情,如果有人有想法,我会很高兴。

EDIT: The server was having a lot of processes unterminated. 编辑:服务器有很多进程未终止。 Cleaning those solved the problem. 清洁那些解决了问题。 So the problem is related to another application, but I'll improve the logging thanks to your comments. 因此,问题与另一个应用程序有关,但是由于您的评论,我将改善日志记录。

Small problem: you are NOT capturing STDERR, so you won't see the error (you are also not checking $? return code). 小问题:您没有捕获STDERR,因此不会看到错误(您也没有检查$?返回代码)。

You should do 你应该做

my $err = `cp -r $HTML /tssobe/www/tstweb/$subpath/$HTMLDIR1 2>&1`;

to redirect STDERR to STDOUT, or use one of the modules for running commands. 将STDERR重定向到STDOUT,或使用模块之一运行命令。


Large problem: 大问题:

You should not run system commands from Perl for which Perl-native modules exist. 您不应从存在Perl本机模块的Perl中运行系统命令。 In this case: File::Copy::Recursive module. 在这种情况下: File::Copy::Recursive模块。

You can also roll your own directory copied from File::Copy . 您也可以滚动File::Copy复制的自己的目录。

Are you using backticks? 您在使用反引号吗? Add -v to the cp commmand to see something in STDOUT and redirect the STDERR to STDOUT and check the cmd exitcode not the error message in the STDERR. 将-v添加到cp命令中,以在STDOUT中查看内容,然后将STDERR重定向到STDOUT,并检查cmd出口代码而不是STDERR中的错误消息。

What about printing out the command output right after the execution? 在执行后立即打印出命令输出呢?

my $err = `cp -rv $HTML /tssobe/www/tstweb/$subpath/$HTMLDIR1 2>&1`;
my $exitcode = $? >> 8;
warn "Output: $err\nexitcode: $exitcode\n";

It would be better to use qx. 最好使用qx。 Check this: http://www.perlmonks.org/?node_id=454715 检查此: http : //www.perlmonks.org/?node_id=454715

You may also want to quote arguments that may potentially contain any shell special characters, including spaces. 您可能还想引用可能包含任何shell特殊字符(包括空格)的参数。 As shell will do word splitting on the string given to it, if $HTML contains a space cp would get more arguments than you expect. 由于shell会在给定的字符串上进行单词拆分,因此,如果$HTML包含空格, cp将获得比您期望的更多的参数。 Perl has very simple mechanism for that: \\Q\\E . Perl为此具有非常简单的机制: \\Q\\E Here is how you do it: 这是您的操作方式:

my $err = `cp -r \Q$HTML\E \Q/tssobe/www/tstweb/$subpath/$HTMLDIR1\E 2>&1`;

Anything except for alphanumeric will be backslash escaped before passing it to the shell. 除字母数字外,其他任何东西都将在传递给shell之前被反斜杠转义。 And you would provide exactly 2 arguments to cp regardless of what is in those variables. 无论这些变量中有什么,您都将为cp提供正好2个参数。

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

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