2

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?

3
  • 1
    What user is executing the script the php file? Must be some apache, not your user. So it is showing the crontab for apache user. Commented Jun 6, 2013 at 9:02
  • chmod 0777 on that file Commented Jun 6, 2013 at 9:02
  • -rwxrwxrwx 1 kn3l kn3l 47 Jun 6 14:59 test.php* Commented Jun 6, 2013 at 9:04

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

try this

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

Comments

2

Add to first string to test.php

#!/usr/bin/php

Comments

1

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.

1 Comment

Great. Does this answer your question then? If so, you can accept this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.