In this article we are going to talk about Cron Jobs from the scratch. We will discuss several Cron Job related topics like what exactly it is, how to it setup up and much more.
So, here we go…
What is a Corn Job?
Cron job is a Linux utility.
With the help of Cron Jobs a Linux user can schedules a command or task to be executed sometime in the future.
For automate monotonous tasks, it is a useful feature.
So, these scheduled tasks or commands are known as Cron jobs.
With this feature you can easily schedule backups, deleting files, and many other important tasks as well.
Cron jobs scripts that are generally executed are used to modify files or databases.
Other tasks like sending email notifications or that do not modify on the server are also done.
Setting Cron Jobs to run by minute, hour, day of the week, day of the month, month or any of these combinations is also possible.
Elements
- The script that is to be called or executed.
- The command that executes the script on a recurring basis (typically set in the cPanel).
- The action or output of the script (which depends on what the script being called does).
A general format of a cron job is –
Minute(0-59) Hour(0-24) Day_of_month(1-31) Month(1-12) Day_of_week(0-6) Command_to_execute
Cron Job Setup Using cPanel
cPanel is a Linux-based Graphical User Interface (GUI), and today we are going to use it to set a corn job.
Here’s how you can do it –
1) First log in to cPanel.
2) Now search for Cron Job from the dashboard or go to the Advanced menu option and click on Cron jobs.
3) After that you will have to select an email where you’d like to receive Cron output.
4) Next simply leave the email address listed beside Current Email if its valid.
5) In case you wish to use a different email id then enter the id into the form field next to Email and click Update Email once you have finished.
6) Next in the Add New Cron Job section, you can give the command that you wish to run along with its frequency.
7) Here you can list either Linux commands or a file that you want to run at a specific time.
8) Now go to the field labelled Common Settings.
9) Here select the frequency that you would like to run from the options at the drop-down menu.
[su_highlight]Note: This will also update time settings according to your choice.[/su_highlight]
10) Frequency can also be adjusted easily, and Month, Date, Hour and Weekday can also be changed.
11) Click on Add new Cron Job option after you are done making your selections.
Now you have successfully scheduled a command using the Cron Jobs tool within cPanel.
Let us now discuss some examples of commands –
The format to run a Cron Job every minute is:
* * * * * <command-to-execute>
The format to run a Cron Job at every 5th minute is:
*/5 * * * * <command-to-execute>
The format to run a Cron Job at everyquarter hour or 15th minute is:
*/15 * * * * <command-to-execute>
The format to run a Cron Job atevery hour at minute 30 is:
30 * * * * <command-to-execute>
With the help of commas, you can also select multiple time intervals:
0,5,10 * * * * <command-to-execute>
To run a Cron Job everyday:
0 0 * * * <command-to-execute>
Run it every 2 hours:
0 */2 * * * <command-to-execute>
To run a cron job every month:
0 0 1 * * <command-to-execute>
To run a cron job at a specific time of the month:
5 0 * 4 * <command-to-execute>
Run a cron job every 6 months:
0 0 1 */6 * <command-to-execute>
To run cron job every year:
0 0 1 1 * <command-to-execute>
To run a cron job every Sunday:
0 0 * * SUN <command-to-execute>
To run a cron job at 3 am, everyday:
0 3 * * * <command-to-execute>
So here were 13 examples that will help you schedule tasks.
With the above guide you might have now understood the basic usage of Cron Job at Linux system.
Why Use It?
The usage of Cron Jobs has been immense for many users, but let us discuss some relevant usage such as –
- One of the most effective of corn job is to regularly deactivate or delete accounts. This is mostly used for accounts with membership, where after the expiration date, you can set Cron job to deactivate it for no further chargers.
- Sending of daily newsletter e-mails with Cron Jobs become an easy task.
- With the help of Cron Jobs you can easily erase cached data files in regular intervals.
- Checking your website for broken links is also possible with Cron Jobs.
- Scheduling tasks to run from a command line script can be done, rather than using web script to run it.
- Easy tasks like fetching your most recent Tweets and put it in specific text files is also done by Cron Jobs.
How to Prevent Cron Job Collision?
As many Cron job runs together, you will not want any of them to collide if they are taking longer time to run than the frequency itself.
Let’s consider an example, that you have a cron Job set to run every minute but sometimes it might take longer than a minute to run.
This can result in the same cron script to run before the previous one.
Due to this too many processes can run which can result in a possible crash in the server.
Well, you can solve it with the help of file locking or non-blocking (LOCK_NB) type of file locks.
By adding this command to the corn job script you can use file locking.
$fp = fopen(‘/tmp/lock.txt’, ‘r+’);
if(!flock($fp, LOCK_EX | LOCK_NB)) {
echo ‘Unable to obtain lock’;
exit(-1);
}
/* … */
fclose($fp);
When you use file locking, they basically block the script if there is an existing lock.
They would also release after the lock doesn’t exist anymore.
Conclusion
So, that all for Cron Jobs. Hope this article was helpful and gave you a subtle understanding of this scheduling command feature.
Now give it a try and starting using Cron job, by scheduling tasks on your system.