Ok this is not going to be a catch all end all to the woes of the cron settings. but this is my attempt at providing some information on how to test that your cron jobs are firing. our first example is utilizing cron from public_html or public access directory on your server. in our example that is public_html. then we will setup a test cron from a subdirectory and show how to write the cron command for each example.
we are going to create a php file and name it mycron.php upload this to your server here is my example:
<?php
//Filename: mycron.php
mail ("cron@terabyte-hosting.com", "Cron Successful Public HTML!", "Hello World from mycron.php!");
?>
you need to change the email address to your email address on this file.
now we are going to fire this file from the browser http://yourdomain.com/mycron.php this should trigger an email to your inbox.
as of now we have not setup a cron job. a cron is the automation process of what you just done by calling the file from the browser. now we will create the cron job. in our example we are using cPanel, depending on your control panel you may have different options. if you have no control panel at all, and are editing from the crontab that would be different as well. we will try to get to that.
in cpanel under the cron job settings. img add-new-cron-job
for testing purposes we will set this to fire every minute, not a good practice by any means, but for testing, we can get away with it.
so our cron command will be:
php -q /home/username/public_html/mycron.php (change username to your username on your server)
because we are running FCGI we initiate the command with php the -q means to do this quitely then you see the path to our test php file that we want cron to fire.
this --> php -q /home/username/public_html/mycron.php goes in the text area following the word Command: (change username to your username on your server)
then we drop down the common settings, and opt for --> Every Minute (*****)
and hit add new cron job button
now this should start firing emails at you every minute. annoying as all get out, but this is a testing case study, so we have to endure these nuances in life.
the above setup will fire the php file from your public root or www folder in our case again, we are using public_html. now we will demonstrate how to to fire the same file from a subdirectory, and how to setup the cron job on cpanel so that it knows where to find the php file for testing purposes.
for this demonstration, we will use similar syntax but we want to be able to determine where the file is firing from so we make a few edits, one of which is to create a subdirectory ==> croncourse and in this subdirectory we will place mycron2.php. so we will be firing from the browser --> http://yourdomain.com/croncourse/mycron2.php and then setup the cron job to run this file from this subdirectory.
--> <?php
//Filename: mycron2.php
mail ("cron@terabyte-hosting.com", "Cron Successful CronCourse!", "Hello World from croncourse/mycron2.php!");
?>
so your subdirectory croncourse has been created, you have added the file mycron2.php to the subdirectory croncourse. make sure again, you change the email address example to your own email address.
same as procedure one, we will navigate to the cron job settings on cpanel. our cron command is slightly different for firing a file from a subdirectory:
add this to your cron command text area --> cd /home/username/public_html/croncourse/; php -q /home/usename/public_html/croncourse/mycron2.php (change username to your username on your server)
now we are going to drop down the common settings and opt for Every Minute(*****). again this is for testing purposes and running a cron command every minute is bad practice.
so we will explain what we have done here, and what
cd /home/username/public_html/croncourse/; instructs that we are to change directory, and the directory we are changing to is on the absolute path /home/username/public_html/croncourse/ the semi-colon is a terminator or stop sign for the cron command
now we instruct that we are using php and again utilzing the flag -q for quitely we tell cron the path to our file and our filename to fire --> /home/username/public_html/croncourse/mycron2.php
save your settings, and you will start getting emails every minute, and the subject will be Cron Sucessful CronCourse and the message in the email will be Hello World from croncourse/mycron2.php
if you think this was hard to read, you should try typing this crap. at any rate, hope this helps somebody.
now i am off to delete the thousands of emails i got while running this test in order to provide this descriptive.