
What Are Cron Jobs?
Cron jobs are scheduled tasks executed at regular intervals of time by the user. They execute scripts coded in programming languages. Cron jobs have multiple applications in daily site management.
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.
Host PHP Websites with Ease [Starts at $10 Credit]
- Free Staging
- Free backup
- PHP 8.0
- Unlimited Websites

How do I run a cron job in PHP?
Timing – set the weekday, months, days, hours and minutes.
Execute – the cron job needs to be called in PHP to run – that’s located at /usr/bin/php path.
Script Path – the path of the file you want to run.
Output – you are allowed to add the cron output to a file or discard it. /dev/null 2>&1 will discard.
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.
Common Settings:
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:
if(issset($_SERVER['REMOTE_ADDR']))die('Permission denied.');
Conclusion:
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.
Q. How do I run a cron job in PHP ?
A: Scheduling cron jobs in PHP is much easier if you are using the Cloudways platform instead of cPanel. When you log in to the platform, there is the button “Add Cron Job” under the Cron Job section. Just click on the button, you have to set its frequency using the drop-down and provide the link to the script of the task you want to schedule and click the Apply button. Just like that, within a few minutes, your cron job is all set and ready to be used.
If you want to manually set the cron job, then you can do that as well using the Advanced section.
Q. Is cron job secure ?
A: Cron job is secure but It is just another way for the assailant to take control of the system and sometimes components connected to it. It usually depends on the scripts. If a malicious user overwrites these scripts you’ve probably got bigger problems, Resulting in leaving open doors for the virus to enter and compromise the entire system.
Ahmed Khan
Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works.