Cron Job Setup and Testing Example

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.

 

 

add-new-cron-job.png · 6.4K · 758 views
When a GIG is not enough --> Terabyte Dolphin Technical Support - Server Management and Support
Quote · 20 May 2011

Everything worked to an exact science following your tutorial. I had success on every demonstration. Then I simply changed it so it was the dolphin cron.php instead of your mycron2.php and of course added the correct file path. Problem is now I get this every minute in my email once its attached to the dolphin cron.


/bin/sh: line 1:  3787 Segmentation fault      php -q /home/www/#######/periodic/cron.php

 

#######= just me hiding my info.

 

This has me stumped. I am running 7.0.9

 

Here is the command I am using.

cd /home/www/#######/periodic/; php -q /home/www/######/periodic/cron.php

 

Keep in mind also I run a in house control panel. It is not the typical cpanel most you are familiar with. For another web script I run I have a cron that fires once a day in a sub directory that does not require that type of command. I simply put "php" in front of the file patch to the cron file and that its works everyday. Something has changed and my dolphin cron is not firing to empty my que for messages so I tried your tutorial.

 

Thanks for taking you own time to post this here. Im still broken but it feels like progress.

Quote · 24 Feb 2012

I upgraded to php 5.3.9 and enabled the APC cache accelerator prior to these cron problems. Anyway they could be related. Currently the only way I can get the cron to fire is manually or using your sub directory example with arrives at my email every minute but only as a "Segmentation fault" every minute. I used wikipedia to learn a little bit about a segmentation fault which suggest a server side problem. But using the example provided that have been provided above works perfectly so it has to be the cron.php itself and not my server I would think. I've turned on and off APC and it of course does not effect the cron. I know that sounds dumb but the best I can tell the only changes I have done is upgrade php and enable APC so just checking my tracks.

The only thing I have stuck in my head is that the php upgrade has caused something in the cron.php not to function as it was. Aggravating but I will step back down to php 5.2 and see if they makes any difference just as test. My searches have pulled very few results for anything helpful on the cron returning a segmentation fault.

Quote · 25 Feb 2012

It wont even fire manually.. The cron does absolutely nothing. I can not find a way to get to fire. Example crons provided here fire every time but even a fresh copy of the dolphin cron.php will not fire. Im sick of dealing with this today. Its been secluded down to this one cron job. Its probably something so simple but I have a throbbing headache and important stuff that is not happening because of this cron.

Quote · 25 Feb 2012

I have this working now.

Reference- http://www.boonex.com/forums/?action=goto&my_flags=1#topic/periodic-cron-php-stopped-firing.htm

but.. without the .htaccess file in the periodic folder

Quote · 25 Feb 2012

Thanks.

Quote · 27 Jul 2017
 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.