Fri. Aug 14th, 2020

How to use Crontab command in Linux

2 min read

Each operating system has its own function of automatically starting the program on time, Windows has its task plan, and Linux‘s corresponding function is crontab.

The crontab command is commonly used in Unix and Unix-like operating systems to set instructions that are executed periodically. This command reads instructions from the standard input device and stores them in a “crontab” file for later reading and execution. Normally, the instructions stored in crontab are activated by the daemon, and crond often runs in the background, checking every minute to see if there are scheduled jobs to be executed. This type of job is commonly referred to as cron jobs.

Use

The format of crontab is as follows:

f1 f2 f3 f4 f5 program

f1 is the minute, f2 is the hour, f3 is the day of the month, f4 is the month, and f5 is the day of the week. The program indicates the path to execute the program.
  • f1 is *, program is executed every minute when f2 is *, program is executed every hour, and so on.
  • f1 is a-b, it will be executed from the a minute to the b minute. When f2 is a-b, it will be executed from the a to b hour, and so on.
  • f1 is */n, it is executed once every n minutes, f2 is */n, it is executed once every n hours, and so on.
  • f1 is a, b, c, …, it means that a, b, c, … minutes will be executed, when f2 is a, b, c, …, it means that a, b, c … will be executed in hours, and so on.

The usage of crontab is actually very easy to master. Knowing how to use crontab can greatly help website and server maintenance, such as regular backups and server optimization.