What Are Cron Jobs?
Cron is a scheduler which is time-dependent. This feature is available in Mac, FreeBSD and Linux as well. The scheduled activities which are performed through them are referred to as cron jobs.
Where Is It Used?
A cron job in PHP powered systems, in particular, is often used to ensure timely execution of important tasks including executing or scheduling a code snippet. They are often used for system maintenance. However, cron jobs are equally useful at the application level. It happens often that a few tasks may need to scheduled in a web application.
How Does It Work?
A Cron ‘Daemon’ usually exists on the systems mentioned above. Daemon is basically a program which keeps on executing all the time in the background. This program is actually responsible for execution of scheduled tasks as specified. A configuration file called ‘Crontab’ lies in the schedule. All the timers and tasks info lies there.
7 Scenarios When Cron Jobs Can Be Useful for You:
As mentioned above, cron jobs are more useful for server admins in general. However, for a web developer, this feature can be useful in the following cases:
- If you own a website which provides memberships. You may schedule cron jobs with respect to their date of expiry. Hence, you may either deactivate or delete accounts according to the date of expiries.
- If you own a blog, then cron job is perfect for you to get the maximum reach by automating newsletter emails through cron jobs.
- If you want to save every web page hit in a table of a database, then cron job is perfect for you. It will help you to store daily traffic summaries in your database.
- Cron job can facilitate you to erase cached data files.
- If you are responsible for SEO and have concerns about your million pages website broken links, it is perfect for you. You can auto check broken links in your website and send yourself a daily report via scheduling.
- You can perform long running tasks from command line script by Cron jobs such as emails sending in large or encoding videos.
- If you are a social media marketing specialist, Cron job is perfect for you. You may fetch recent tweets which will be cached in a text file.
Many hosting providers offer their own interface for adding cron jobs. Cloudways, which is a managed cloud hosting platform, also provides an option for adding cron job in their application. You may see below the interface which Cloudways provides for adding cron jobs.
You might also like: How To Host PHP On DigitalOcean Using Cloudways
The above interface can be found after launching a server on Cloudways, in the “Application Management” tab. The screenshot above shows that there is no cron job in the application. Just click on “Add New Cron Job” link to add a cron job.
In the screenshot above, you can see a number of fields for adding or updating a cron job. We will explain every field one by one.
First of all, we have the common settings option. It is to specify the Cron job settings. It contains five parts in every option which are
- Minute (Range: 0 to 59)
- Hour (Range: 0 to 23)
- Day of month (Range: 1 to 31)
- Month (Range: 1 to 12)
- Day of week (Range: 0 to 6, Sunday will count as 0)
In this interface, the meaning of Asterisk also needs to be understood. It basically represents the possible number for that position. If it is on minute, then it will make the cron job run every minute.
You can specify the above options as well to schedule the cron job. Then you have the type option as:
Here you have option to schedule cron job either via PHP page or cURL or wget . However, We will only discuss here scheduling cron job via PHP here.
You might also like: PHP 7.1 Powered Hosting Now Available On Cloudways
For the time being, we have selected via PHP page. We have just created a page cron_test.php. It will be the location where cron job will be scheduled. We have given the following code to display automatically at every minute.
In Cron_test.php we have placed the code to show the current date.
Here is the code:
<?php echo "The time is " . date("h:i:sa"); ?>
Next tab, we have in cron job is for advanced settings. It is shown below:
In this advanced tab , you can directly see or edit cron job commands.
Blocking Untrusted Access to Cron Job:
When you write your cron job in a scripting language like PHP, you should make sure that it is inaccessible from the web browser. One of the approaches is to keep it away from web root folder. But most of the developers don’t do that. Another way is to block the access from .htaccess file. You should just block access by putting this line in .htaccess file, if your are keeping it another folder other than the root.
deny from all
Another way of doing this is to put this line at the beginning of the page, but it will disable the use of wget or curl, so better not to use this:
In this article, we have discussed what Cron jobs are, and where they are used. Further, we discussed how backend mechanism works, how to schedule cron job in PHP using a managed cloud hosting platform.
To test out the efficiency of cron jobs, sign up for your free trial of the Cloudways Managed PHP Hosting Platform today.