Search code examples
phpcroncpanel

Setting up a Cron Job on CPanel to executes a PHP script


As implied in the title, the Cron Job is supposed to execute a php file (update.php, to be specific). The php file then writes to a csv file stored in the same directory.

I have the time set to * * * * * so that it executes every minute. The command is written as follows: php -q /home//public_html/wallboard/update.php I don't believe this is causing any errors, though it also doesn't seem to write to the CSV file. When I visit update.php in a browser, however, it executes and writes to the CSV file immediately. I'm not experienced with Cron Jobs and I'm sure there's an issue, but I don't know what exactly that issue is. Let me know if you have suggestions/questions. Any help is appreciated!

Current Command:

* * * * * usr/bin/php -q /home/<user>/public_html/wallboard/update.php

update.php:

<?php

include('lib/HelpDeskView.php');
include('lib/WallboardDisplay.php');
include('helpdesk.csv');
$helpdesk = new HelpDeskView();
$text="\r\ntest,test,test";
file_put_contents( "helpdesk.csv" , $text, FILE_APPEND);

Solution

  • Since your script resides in your public_html directory you can use wget for your Cron Job

    wget -O - -q https://yoursite.com/wallboard/update.php
    

    -O - output is written to the standard output in this case it will go to the email address you specify in CPanel

    -q quiet mode