perl从cron运行时不执行壳命令

发布于 2025-02-08 08:22:35 字数 439 浏览 1 评论 0原文

我有一个perl脚本,该脚本从命令行调用PHP脚本,并通过电子邮件发送输出。从命令行执行时,该脚本正常工作,但是通过Cron执行时会失败。我尝试从我自己的crontab以及root cron中执行脚本,结果相同。

这是脚本:

#!/usr/bin/perl
my $file = "/app/testing/testfile.php";
my $output = `php $file`;
#Sending e-mail here

这是它正在调用的php脚本:

fopen('/app/testing/log.txt', 'w');
fwrite('hi');
echo 'hi';

从命令行运行时,我会收到带有“ hi”一词的电子邮件,并且文件具有hi hi。从Cron运行,我收到一个空白的电子邮件,并且该文件未修改。

I have a Perl script which calls a PHP script from the command line and sends the output via e-mail. This script works fine when executed from the command line, but fails when executed via cron. I have tried executing the script from my own crontab, as well as the root cron, same result.

This is the script:

#!/usr/bin/perl
my $file = "/app/testing/testfile.php";
my $output = `php $file`;
#Sending e-mail here

And this is the PHP script that it is calling:

fopen('/app/testing/log.txt', 'w');
fwrite('hi');
echo 'hi';

When run from the command line, I get an e-mail with the word "hi" and the file has the word hi. Run from cron, I get a blank e-mail and the file is not modified.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2025-02-15 08:22:35

提供到php的完整路径:

my $output = `/path/to/php $file`;

这是因为cron使用一个相当简化的环境,具有简约$ path

另外,在cron中源来源

03 */1 * * * source ~/.zshrc &> /dev/null ; /path/to/my_script.pl

Provide full path to php:

my $output = `/path/to/php $file`;

This is because cron uses a rather simplified environment, with a minimalistic $PATH.

Alternatively, source your .*rc file in cron, but that may be an overkill:

03 */1 * * * source ~/.zshrc &> /dev/null ; /path/to/my_script.pl
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文