Deac Karns
Deac Karns

Reputation: 1196

php exec() not running via a cron job

I have been banging my head against this for hours now.

I am executing a php script via a cron job, everything in the script works except for the exec() method.

<?php
    exec('gpio write 7 0');

    // Open the file to get existing content
    $current = file_get_contents('log.txt');

    // Append a new person to the file
    $current .= get_current_user().' - '.date('H').":".date('i')." - gpio write 7 0\n";

    // Write the contents back to the file
    file_put_contents($log, $current);
?>

If I execute the php script directly from the terminal it works with both the pi user and with the root user.

The data that is being written to the log.txt file when the cron job runs looks fine to me, heres a sample:

root - 00:16 - gpio write 7 0
root - 00:17 - gpio write 7 0
root - 00:18 - gpio write 7 0
root - 00:19 - gpio write 7 0

I have tried giving the php file that is to be executed both 755 and 777 permissions with no luck.

This is what I have when I execute sudo crontab -e

*/1 * * * * /usr/bin/php /var/www/check_time.php

Any help would be greatly appreciated.

Thanks in advance.

Upvotes: 2

Views: 871

Answers (2)

Wesley
Wesley

Reputation: 344

You should be specific with your path for gpio and give the exact location so cron knows where to find it, /usr/local/bin is not imported by cron:

/usr/local/bin/gpio

So it will then be:

exec('/usr/local/bin/gpio write 7 0);

Upvotes: 3

brettywhite
brettywhite

Reputation: 751

on the line

exec('gpio write 7 0);

should it be

exec('gpio write 7 0');

? It appears that the the ' was never closed.

Upvotes: 0

Related Questions