Cron job: comprehensive guide 2020 for newbies

access_time
hourglass_empty

There are many ways to work effectively. For example, for busy website developers, they will take advantage of automated processes to handle repetitive tasks. If you use an operating system like Unix, the cron job can save you time by processing tasks automatically.

In this article, we will explain what a cron job is, a basic job of cron job, and how to use cron to schedule a task.

What is cron job?

Cron is a program to handle repetitive tasks at a later time. Cron Job issues an order to schedule "work" for a specific action, at a specific time that needs to be repeated.

This is how it works:

If you want to schedule a one-off job, at another later time, you use another command. But for routine jobs, cron is the perfect solution.

Cron is one daemon, meaning it works in the background to perform tasks that do not require interaction. In Windows, you are familiar with the background process called Services.

The daemon is always ready and waiting until the command tells it to run a certain task - on a computer or from other computers on the network.

The cron file is a simple text file containing commands that are run at a specified time. The default crontab file in the system is / etc / crontab and located in crontab directory /etc/cron.*/. Only the system administrator can edit the crontab file on the system.

However, because operating systems that support multiple users like Unix, each operating system can create its own crontab file. It can run jobs whenever it wants. The cron daemon will check the file and run the command on the system platform.

With cron jobs, you can automate system maintenance, monitor disk space, and backup settings. Its nature is therefore suitable for machines that operate 24/7 - like servers.

Cron jobs are most often used by system administrators but it is also useful for webmasters. For example, use cron to deactivate an expired account, check for broken links, or send bulletins to target users.

The basics of Cron job

You can create and edit cron jobs using different methods. In this tutorial, we will show you how to do it using the Linux Shell Prompt (Terminal).

If you have a VPS on Hostinger, go to the VPS admin page to get server access information via SSH. If you have problems, you can review it PuTTY instruction for connecting SSH.

Here are some tasks that cron jobs can perform:

If you want to edit the crontab file of the current user, enter crontab -e in terminal:

crontab

It will return results like this:

tab cron results

If used vi editor, you can learn more vi commands basic to get it right.

If you want to edit crontab of other users, you can type crontab -u username -e. You need to run this command with superuser, which means you need to enter: sudo su before executing this command.

crontab -u username -e outcome cron job

Other operations allow you to view cron files if it is created. You just need to enter crobtab -I. If there are no files, you will see this result:

crontab -l result cron job

Alternatively, if you want to see a list of crontab files of other users, you can type crontab -u username -I under superuser:

cron job crontab -u username -l result

Except for knowing basic tasks, basic syntax is also important to remember.

Basically, crontab file contains 2 parts: scheduler and command. Here is how to write the command:

* * * * * /bin/sh backup.sh
  • ***** / bin / sh backup.sh cronjob means it will run the backup every minute
  • 30 18 * * * rm / home / sydtesting / tmp / * means it erases files tmp out of / home / sydtesting / tmp every day at 6:30 PM.

Let's find out more details about Cron Job

The correct way to write Cron syntax

As mentioned earlier, the crontab file has 5 fields - each field is represented by an asterisk. They are used to determine the date and time of certain tasks that are set to repeat operations.

5 schools of Crontab

  • Minute - minutes of the time the command will run, between 0 and 59
  • Hour - based on the time the command will run, between 0 and 23
  • Day of the month - based on the day of the month you want to run the command, between 1 and 31
  • Month - based on the month in which the particular command ran, between 1 and 12
  • Day of the week - based on the day of the week you want to run the command, between 0 and 7

In addition, you need to use the correct character for each crontab file.

  • Asterisk (*) - to specify all parameters that are scheduled
  • Comma (,) - to maintain 2 or more executions of a command
  • Hyphen (-) - to specify the time interval for setting a time to execute an instruction
  • Slash (/) - to create specific break periods
  • Final (L) - for a specific purpose of specifying the last day of the week in the month. For example, 3L means the last Wednesday.
  • Day of the week (W) - to determine the nearest weekday. For example, 1W means if day 1 is Saturday, the command will run on Monday (Day 3)
  • Hash - to determine the day of the week, followed by the number running from 1 to 5. For example, 1 # 2 means the second Monday.
  • Question mark (?) - leave a space

11 examples of syntax of Cronjob

Now you know how to write the syntax correctly. We will give you specific examples to understand the above rules better.

Before proceeding, make sure the output of the command is automatically sent to your email account. So if you want to stop receiving these emails, you can add them > / dev / null 2> & 1 into the syntax like the example below:

0 5 * * * /root/backup.sh >/dev/null 2>&1

If you want to email the output to a specific account, add MAILTO and then the email address. Here is an example:

MAILTO="myname@hostinger.com"
0 3 * * * /root/backup.sh >/dev/null 2>&1

Here are more examples of syntax:

ExpressionMeaning
0 0 * * * / bin / sh backup.shTo run the backup database in the middle of the night, and to run after 1 day.
0 6.18 * * * / bin / sh backup.shTo run database backups 2 times a day at 6AM and 6PM.
0 * / 6 * * * /scripts/monitor.shTo enforce monitoring every 6 hours.
* / 10 * * * * /home/user/script.shTo run cron job for script file located in the main directory every 10 minutes.
0 * 20 7 * / bin / sh backup.shTo run the backup database every hour of July 20.
0 0 * * 2 * / bin / shTo run the backup database in the middle of the night every Tuesday.
* * * 1,2,5 * /script/script.shTo run the command in January, February and May.
10-59 / 5 5 * * * /home/user/script.shTo run the command every 5 minutes at 5AM, start at 5:10 AM.
0 8 1 * / 3 * /home/user/script.shTo run quarterly orders on 1st at 8AM.
* * * * * /scripts/script.sh; /scripts/scrit2.shTo schedule multiple jobs on independent cron jobs.
@reboot /scripts/script.shTo run certain tasks every time you start the system.
0 0 1 * * /home/user/script.shTo run the order on the first day of each month.

Epilogue

The automatic job scheduler setting is not only convenient, but also helps you to execute actions in a timely manner.

Cronjob is a great way to manage tasks against system administrators or other professions like web developers. You need to use the right command and choose the right time.

Here are some basic commands:

  • $ crontab e - to create and edit crontab files
  • $ crontab -u username -e - to edit crontab files of other users with superuser access
  • $ crontab -l - to view a list of crontab files of the current user.
  • $ crontab -u username -l - to view a list of crontab files of other users.

Now try Cron Job and let it run automatically a command you like to see.

0 Comments

seo marketing wordpress seo seo hosting seo and marketing word press seo wordpress and seo wordpress marketing hosting seo seo press pro market seo seo & marketing seo e marketing e marketing seo seo pro wordpress marketing & seo seo di wordpress wordpress seo host hosting and seo wordpress hosting seo wordpress seo wordpress wordpress for marketing seo press wordpress marketing for seo
×