crontab issue when using exec php script?

风格不统一 提交于 2019-12-04 16:37:40
fedorqui

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.

You would have to execute the script in this fashion:

25 15 * * * /usr/bin/php /var/www/cronjob/helloworld.php > /var/www/cronjob/cron.log

Make sure that /usr/bin/php is valid on your system.

try this

<?
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
?>

Add to first string to test.php

#!/usr/bin/php
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!