crontab问题时使用exec php脚本? [英] crontab issue when using exec php script?
问题描述
我使用elementaryOS(基于ubuntu)
当我运行逗号行时:
kn3l @ kn3l:/ var / www / cronjob $
25 15 * * * /var/www/cronjob/helloworld.php> /var/www/cronjob/cron.log
它适用于带终端命令行的crontab。 / p>
但是当我用这样的代码(test.php)使用PHP时:
-rwxrwxrwx 1 kn3l kn3l 47 Jun 6 14:59 test.php *
测试。 php
<?php
$ output = shell_exec('crontab -l');
echo< pre> $ output< / pre>;
我转到 http://localhost/test.php
为什么我没有看到像命令行的任何返回?
任何人都可以帮助我?
您的用户crontab中有以下行:
25 15 * * * /var/www/cronjob/helloworld.php> /var/www/cronjob/cron.log
在crontab中,有必要指明哪个二进制文件正在执行剧本。因此, hw表示,您需要将其替换为
25 15 * * *< path to php> /var/www/cronjob/helloworld.php> /var/www/cronjob/cron.log
获取此< path c。
test.php
文件,考虑到运行php脚本的用户正在执行命令 crontab -l </ code>。
我做了一个测试:
$ ps -ef | grep apac
... / ...
www-data 1348 1332 0 09:50? 00:00:00 / usr / sbin / apache2 -k start
-data用户正在运行apache。
我在 / var / www
中添加了您的文件:
<?php
$ output = shell_exec('crontab -l');
echo< pre> $ output< / pre>;
?>
执行时没有出现。为什么?因为 www-data
没有crontab:
$ sudo crontab -l -u www-data
没有crontab的www-data
我添加了一行: / p>
$ sudo crontab -l -u www-data
* * * * * touch / tmp / tt
,现在的php页面显示:
* * * * * touch / tmp / tt
/ h3>
事情不是你的脚本不能正常工作,只是显示一个空的内容作为crontab为用户 www-data
为空。
I am using elementaryOS (base on ubuntu)
When I run commaand line:
kn3l@kn3l:/var/www/cronjob$
25 15 * * * /var/www/cronjob/helloworld.php > /var/www/cronjob/cron.log
and it works for crontab with terminal command line.
BUT When I using PHP with my code like this (test.php):
-rwxrwxrwx 1 kn3l kn3l 47 Jun 6 14:59 test.php*
test.php
<?php
$output = shell_exec('crontab -l');
echo "<pre>$output</pre>";
I go tohttp://localhost/test.php
Why I did not see any return like the command line?
Anyone could help me?
You have in your user crontab the following line:
25 15 * * * /var/www/cronjob/helloworld.php > /var/www/cronjob/cron.log
in crontab it is necessary to indicate which binary is executing the script. So as hw indicates, you need to replace it to
25 15 * * * <path to php> /var/www/cronjob/helloworld.php > /var/www/cronjob/cron.log
get this <path to php>
with which php
.
Regarding your test.php
file, take into account that the command crontab -l
is being executed by the user running the php script. That is, the user running your local server.
I did a test:
$ ps -ef | grep apac
.../...
www-data 1348 1332 0 09:50 ? 00:00:00 /usr/sbin/apache2 -k start
so in my case it is www-data the user running apache.
I added your file in /var/www
:
<?php
$output = shell_exec('crontab -l');
echo "<pre>$output</pre>";
?>
And nothing appeared while executing it. Why? Because www-data
has no crontab:
$ sudo crontab -l -u www-data
no crontab for www-data
I added a line:
$ sudo crontab -l -u www-data
* * * * * touch /tmp/tt
and now the php page shows:
* * * * * touch /tmp/tt
To sum up
The thing is not that your script is not working properly, is just that is showing an empty content as the crontab for user www-data
is empty.
这篇关于crontab问题时使用exec php脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!