Cron is a time-based job scheduler in Unix-like computer operating systems. The name cron comes from the word "chronos", Greek for "time". Crontab jobs allow users to schedule when a particular script is executed based on matching month, day, hour, and minute.
I’m using a crontab job or “cron job” to schedule when a PHP “membership renewal reminder” script is run.
The PHP script, which is run once a day, compares the expiresDate with the current date for each member. If the number of days until expiration matches a preset number, the member is sent an email reminding them that their membership is about to expire.
Crontab jobs are pretty particular in how they’re set up, and ISPs differ in the way that you access and input the information to set up a cron job, so you may have to get in touch with their tech support to get things operating as they should. My ISP (IXWebHosting) sets up cron jobs in the ftp management section of their interface.
There’s specific syntax for entering the scheduling information into a cron job request. At the end of this recipe, you’ll find the acceptable parameters from my web host IXWebHosting, taken straight from their “help” screen.
NOTE: You'll have to insure that the script that you're trying to run with the conjob has write permission
CREATING AND RUNNING A CRON JOB There are 3 pieces of information necessary for creating and running a cron job 1) the scheduling information 2) the path to your server’s php interpreter (obtained from your web host), and 3) the path to the php file to be executed
I needed to run my script once a day at 2 minutes after midnight, so my scheduling parameters are: Minute: 2, Hour: 0, Day of month: 1-31, Month: 1-12, Day of week: 1-7
After you’ve set up all your scheduling parameters, either put both the path to your server’s php interpreter, and the path to your PHP script in your crontab command line as in this example (Note: The path to your server’s php interpreter will be different, so check with your web host, and don’t forget the space between the 2 segments.):
*** Note: if you have issues with sending mail using cron jobs, see the recipe under Bugs and Fixes called: "Cron issues sending mail with CMSB 3.50 or earlier, and PHP 7.2 after a Bluehost server upgrade"
/hsphere/shared/php5/bin/php -q /your_server_path/mailtest.php
or, put the path to your server’s php interpreter at the very top of your php script with no blank spaces or lines before it, like this (note: The path to your server’s PHP interpreter will be different, so check with your web host.):
#!/hsphere/shared/php5/bin/php-cgi -q
And insert only the path to your php script in the command line, like this:
/your_server_path/mailtest.php
TESTING SENDMAIL To test if your sendmail is working you can create a .php file called mailtest.php with this simple php sendmail test script. When you refresh the page it should send the email.
<?php $to = "your_email@your_host.com"; $from = "you@sendmail_test.com"; $subject = "Testing Sendmail"; $txt = "Testing Sendmail from PHP Script"; mail($to,$subject,$txt,"From: $from", "-f$from"); ?>
NOTE: You'll need to make sure that this file has a permission of 755 (read, write, execute) or the file may not run when called by the cron manager (your cron daemon confirmation email will show an "access denied" error).
TESTING YOUR CRON JOB Once you’re sure that sendmail is working, you can use the script to test your cronjob.
CRON JOB CONFIRMATION EMAILS More than likely, your Cron Daemon will send a confirmation email to an address you choose and you can use the information in that to debug your cron job and your scripts
Once you're sure everything is working, you can send the daemon emails to a non existent email address so that they will not clutter up your inbox.
|