What are and how to use Cron Jobs?

What are and how to Use Cron Jobs
Photo by Pexels from pixabay.com
Want to make sure your daily backups are running? Take a look at PushMon, the easiest way to monitor your cron and scheduled tasks.

What are Cron Jobs?

Cron job is a scheduling tool that allows you to control tasks to be performed at preconfigured times.

Through it you can configure automatic tasks, that is, you can define routines to be executed at a specific time.

Cron Jobs are like Windows Scheduled Tasks: tasks are run automatically from X to X times. The term “Cron Job” (also called cron) is more connected to UNIX systems than Windows itself. So what I’m going to talk about here only fits into Linux. If you use Windows just take a look at the scheduled tasks you will have a complete interface to work with Scheduled Tasks.

Why use a Cron Job on an online site / system?

The most common purpose of a Cron Job is the backup routine. Scripts that run daily (or even longer) and back up the site and the database.

You can create a cron for almost anything, but they are usually for updating, cleaning, backup and so on.

Cron jobs are also often used to check email, send email or check for updates. If a Cronjob is used on a server (web), this is often a backup database script, or a PHP or perl to run a command.

For those who work with website and webservers management, the idea of scheduled tasks is common or at least familiar. Cron jobs make up a technology responsible for running scheduled tasks on a server.

The use of cron jobs or scheduled tasks is great for performing actions that should be repeated on your site and server. As we have seen, the most common is checking for updates, performing backups, etc.

How to create a Cron Job?

If your site runs on some specialized server and you have a control panel like cPanel recommend you take a look there because there is a ready web interface to manage the crons, if you do not have this panel or do not have access to it, you will have that go straight to the shell / terminal of your server and start doing it manually.

It is also important to do the following tasks:

  • Define what cron will do, what it will handle, either in the database or in files on the server.
  • Create / test scripts with routines and functions that do the manipulations that were initially defined. Example: Create routines that read the available XML and update the values in the database.
  • CRON: to create a scheduled task do the following tasks:
    • Click the “Cron Jobs” icon inside cPanel, under Advanced. Make sure the current email address is valid. Otherwise, enter a new email and click the “Update Email” button.
    • Select the setting you want.
    • Alternatively, you can adjust the time settings individually. Minute, hour, day, month, and day of the week can be modified to achieve your goal.
    • In the input box to the right of “Command, enter the name” of the file type, then add a space and provide the path to the file you want the command to be executed. (Link your script)
    • Click the “Add New Cron Job” button. You have just created a cron job to run a file on a particular date and time with the desired repeat.
    • Scheduled Tasks are easy to edit and delete. Click the “Cron Jobs” button inside the cPanel. Scroll down to the last section called “Current Jobs Cron.” Find the correct cron job and click “Edit” or “Delete” under the actions. Editing does not have good automated tools, so it may be easier to copy the command, delete cron job and recreate.