How to set up system scheduled tasks on Linux

WBOY
Release: 2023-07-05 14:46:29
Original
10493 people have browsed it

How to set up system scheduled tasks on Linux

In Linux systems, we often need to perform some tasks regularly, such as backing up databases, cleaning log files, etc. In order to implement these scheduled tasks, we can use the scheduled task tool of the Linux system-crontab.

1. Understand crontab
crontab is a scheduled task management tool under the Linux system, used to perform regular tasks. It will perform specified tasks according to the scheduled time and can run automatically in the background, which is very convenient.

2. Set up scheduled tasks

  1. Open the terminal and enter the command crontab -e to enter the scheduled task editor.
  2. In the editor, we can set up new scheduled tasks. Each line represents a task, and the format of each line is as follows:

    分钟 小时 日期 月份 星期 要执行的命令
    Copy after login

    Among them, the minute value range is 0-59, the hour value range is 0-23, the date value range is 1-31, and the month value range is The value range is 1-12, and the value range of the day of the week is 0-7 (0 and 7 both represent Sunday).

    For example, the following example is to back up the database regularly at 3 pm every day:

    0 15 * * * /usr/bin/mysqldump -u username -p password database > /path/to/backup.sql
    Copy after login

    In this example, 0 15 * * * represents the 15th hour of every day (i.e. 3 pm), /usr/bin/mysqldump -u username -p password database > /path/to/backup.sql is the command to be executed to back up the database to /path/ to/backup.sql file.

  3. After editing, press Ctrl X, then press Y to save and exit.

3. Commonly used crontab operation commands
When using crontab, you can also use some commands to manage scheduled tasks.

  1. crontab -l: List the current user’s scheduled task list.
  2. crontab -e: Edit the current user's scheduled task list.
  3. crontab -r: Delete the current user's scheduled task list.
  4. crontab -u username -l: List the scheduled task list of the specified user.
  5. crontab -u username -e: Edit the scheduled task list of the specified user.
  6. crontab -u username -r: Delete the scheduled task list of the specified user.

4. Other precautions

  1. When writing scheduled tasks, pay attention to the execution path of the command. It is best to use absolute paths to avoid commands not being found due to environment variables and other issues.
  2. In scheduled tasks, wildcards can be used to achieve more flexible task scheduling. For example, * represents all possible values, and */n represents every n time units.
  3. crontab supports using the @reboot keyword to implement tasks that are automatically executed when the system starts. For example:

    @reboot /path/to/your/script
    Copy after login

The above are the methods and examples of setting up system scheduled tasks on Linux. By properly setting scheduled tasks, we can realize automated operation and maintenance operations and improve efficiency and stability. Hope this helps!

The above is the detailed content of How to set up system scheduled tasks on Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template