Ubuntu-16.04 [SOLVED]: Permission denied or Something when trying to run PHP script as a CRONJOB to create a .txt file

Ubuntu-16.04 [SOLVED]: Permission denied or Something when trying to run PHP script as a CRONJOB to create a .txt file

Home Forums Ubuntu 16.04 Ubuntu-16.04 [SOLVED]: Permission denied or Something when trying to run PHP script as a CRONJOB to create a .txt file

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36640

    Anonymous

    QuestionQuestion

    I am running Ubuntu 16.04. I am trying to run a PHP script located at var/www/html/CronScripts/ as a cronjob. All this script does is create a .txt or .out file in the same directory. Here is the script:

     <?php 
    
    $handle = fopen("theFileHandle.out", "a");
    fwrite($handle, "The quick brown fox jumps over a lazy dog.n");
    fclose($handle);
    
    ?>
    

    Now the problem is that, when I execute this script by opening it in the browser, I get the following errors:

    Warning: fopen(theFileHandle.out): failed to open stream: Permission denied in /var/www/html/CronScripts/index.php on line 3
    
    Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/CronScripts/index.php on line 4
    
    Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/CronScripts/index.php on line 5
    

    SO my inference was that if this script is run as sudo, there won’t be PERMISSION PROBLEMS; and since I am ultimately going to run the script in a cronjob, I executed the following command in terminal

    sudo crontab -e
    

    to add the follwoing cronjob to sudo user’s crontab file.

    * * * * * php /var/www/html/CronScripts/index.php > /var/www/html/CronScripts/output.out
    

    After this, a file named output.out WAS created ** BUT IT WAS EMPTY.

    THEN from my websearch I concluded that I was adding cronjob to sudo user’s crontab, but apache2/php are run by user www-data, so that is probably the problem. So I switched user to www-data by executing su -s /bin/bash www-data and then to its crontab I added * * * * * php /var/www/html/CronScripts/index.php > /var/www/html/CronScripts/out.out BUT the output log file out.out is never created.

    So my question is that why am I facing this problem and how do I solve this?

    #36641

    Anonymous

    Accepted AnswerAnswer

    The original problem seems that the user www-data (the one executing apache and so the php script) does not have rights to write in the directory. You can solve that with chown and chmod, giving permission to read and write to the www-data user in this directory.

    That solution could be a security problems, consider using a directory out of the scope of the apache server (e.g /varr/www/anotherplace) and ensure that you dont give execution rights to the files your script create.

    Source: https://stackoverflow.com/questions/47951349/permission-denied-or-something-when-trying-to-run-php-script-as-a-cronjob-to-cre
    Author: miguel-svq
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.