1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Create file with CRON job

Discussion in 'PHP' started by bumbar, Apr 11, 2016.

  1. #1
    Hello,
    I have a problem when creating file with cron job via cpanel.
    In the same file there is a query to a database (INSERT), so surely each hour runs cron.
    But the file ( mycron.txt ) is not created.
    However, if you access the script through a browser, a file is created
    Here is part of the script to create a file:
    
    $myfile = fopen("/home/mysite/public_html/mycron.txt", 'w');
    $txt = "test1\n";
    fwrite($myfile, $txt);
    fclose($myfile);
    
    Code (markup):
    And here is cron
    
    Minute    Hour    Day    Month    Weekday
    0        *          *         *             *
    /usr/local/bin/php -q /home/mysite/public_html/crontest.php  /dev/null 2>&1
    
    Code (markup):
    I am not aware of commands to cron and suppose somewhere error...
    Can anyone help?
    Thank you.
     
    bumbar, Apr 11, 2016 IP
  2. fisasti

    fisasti Active Member

    Messages:
    42
    Likes Received:
    5
    Best Answers:
    2
    Trophy Points:
    58
    #2
    Hi bumbar. I imagine you're going through a permissions issue. I bet the folder is writable for the webserver user but not for the user executing the crontab.
    Fast solution: set the folder to 777 permissions with chmod (chmod 777 filename on the command line).
    Secure solution: set 777 permissions only for the text file (chmod 777 mycron.txt) and change the writing mode of the php file pointer to "a+". That way you don't create the txt file everytime the cron script runs but you add the content to the existing txt file.

    Hope it helps!
     
    fisasti, Apr 11, 2016 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Or better, give the cron-job user access to write to the public_html-folder.
     
    PoPSiCLe, Apr 11, 2016 IP
  4. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #4
    Try with quotes: (no sure though).
    /usr/local/bin/php -q "/home/mysite/public_html/crontest.php"
     
    Vooler, May 12, 2016 IP