Configure scheduled tasks on Linux using Crontab

On Linux systems, you can use crontabcommands to configure scheduled tasks. crontabis a tool for managing user scheduled tasks, which allows you to automatically execute scripts or commands at specified time intervals.

Configure scheduled tasks

To configure a cron job, open the user's crontab file with the following command:

crontab -e

This will open a text editor that allows you to edit the user's cron job.

The format of each line of timed tasks is as follows:

* * * * * command_to_be_executed

There are 5 asterisks ( *), which represent minute, hour, date, month and day of the week respectively. You can specify times using specific numbers or wildcards.

Here are some commonly used examples:

  • *: matches all possible values
  • */n: every n units
  • x-y: range from x to y
  • x,y,z: multiple values

For example, to run a script every day at 8:00 AM, you could use the following cron job:

0 8 * * * /path/to/script.sh

This will execute the script every day at exactly 8 o'clock /path/to/script.sh.

Corn expression

Crontab uses Corn expressions to specify the time interval of scheduled tasks. Corn expression consists of 6 fields, namely: minute, hour, date, month, week, command.

Each field can use a specific value to configure the execution time of the scheduled task. The following are examples of Corn expressions:

  • * * * * *: Execute every minute
  • 0 * * * *: Execute once every hour at the 0th minute
  • 0 0 * * *: Execute once every day at midnight
  • 0 0 * * 0: Executed every Sunday at midnight

In addition to single values, other symbols can be used to define time intervals:

  • */n: every n units
  • x-y: range from x to y
  • x,y,z: multiple values

Here are some more complex Corn expression examples:

  • 0 8-18/2 * * *: Execute every 2 hours from 8 am to 6 pm
  • 0 0 1,15 * *: Execute at midnight on the 1st and 15th of every month
  • 0 0 * * 1-5: Executed every weekday at midnight

You can use appropriate Corn expressions to configure scheduled tasks according to your needs.

Guess you like

Origin blog.csdn.net/qq_39997939/article/details/131579561
Recommended