Perl 脚本不会运行相对路径中的下标
我有一个 perl 脚本:run.pl,每分钟都会被 cron 调用。
run.pl 唯一做的就是调用另外两个脚本:download.pl 和 parse.pl:
#!/usr/bin/perl
use warnings;
use strict;
do 'download.pl';
do 'parse.pl';
print "done!\n";
在 download.pl 和 parse.pl 中有两个打印,其中包含“完成下载”和“完成解析” 现在,我将脚本输出到 /var/log/script.log 并检查脚本是否运行。
run.pl 脚本运行良好,它输出“完成!”到日志文件。但其他两个脚本没有被调用。我认为这是一个相对路径问题,当我使用绝对路径时它起作用。
但这就是问题所在,脚本处于测试阶段,每次都会更改路径,总是更改绝对路径会很混乱。
有没有办法让脚本从相对路径运行?
编辑: 当我自己从命令行使用“perl run.pl”运行它时,它运行脚本没有问题。
I've got a perl script: run.pl that's been called by cron every minute.
The only thing run.pl does is call two other scripts: download.pl and parse.pl:
#!/usr/bin/perl
use warnings;
use strict;
do 'download.pl';
do 'parse.pl';
print "done!\n";
In download.pl and parse.pl are two prints to with "done downloading" and "done parsing"
Now I output the script to /var/log/script.log and check for the script to run.
The run.pl script is doing fine, it outputs "done!" to the logfile. But the other two scripts aren't called. I think it is a relative path problem, it works when I use the absolute path.
But that's the problem, the script is in teststage and changes paths every time, it would be a mess to always change te absolute path.
Is there a way to let the scripts run from the relative path?
Edit:
When I run it myself from the commandline with "perl run.pl" it runs the scripts without a problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,你为什么使用
do
?这并不“调用”程序,你知道!这些是库模块还是程序?
此外,程序应在
$ENV{PATH}
中找到,而库应在@INC
中找到。这些几乎肯定是不同的事情。我的建议是:
$ENV{PATH}
中的另一个程序,请使用system()
或反引号。@INC
中的库,请使用require
或use
。do FILE
保留用于异国情调的目的,但几乎肯定不适用于此处。Um, why are you using
do
? That doesn’t “call” a program, you know!Are these library modules or programs?
Also, programs are expected to be found in
$ENV{PATH}
, whereas libraries are expected to be found in@INC
. Those are almost certainly different things.My advice is:
$ENV{PATH}
, usesystem()
or backticks.@INC
, userequire
oruse
.do FILE
to exotic wizardly purposes that almost surely do not apply here.Cron 作业的运行环境与 shell 登录所看到的环境不同。 cron 作业通常将 cron 用户的主目录作为默认工作目录。
解决此问题的一种方法是使用
FindBin
来定位脚本所在的目录并以此为基础制定你的道路。您最好将
download.pl
和parse.pl
转换为真正的模块,从长远来看,它可能会节省您的一些精力。Cron jobs don't run with the same environment that a shell login would see. A cron job will usually have the cron's user's home directory as it's default working directory.
One option to get around this is to use
FindBin
to locate the directory the script is in and base your paths off of that.You might be better off turning
download.pl
andparse.pl
into real modules, it might save you some effort in the long run.没有帮助吗?
doesn't help?