Home  >  Article  >  Operation and Maintenance  >  How to configure system scheduled tasks on Linux

How to configure system scheduled tasks on Linux

PHPz
PHPzOriginal
2023-07-06 11:58:3916562browse

How to configure system scheduled tasks on Linux

In Linux systems, scheduled tasks are a very common and practical function that can be used to perform certain operations regularly, such as backing up data and cleaning up temporary files. , regularly update software, etc. In this article, we will introduce how to configure system scheduled tasks in Linux systems, and attach corresponding code examples.

There are many ways to implement scheduled tasks in Linux systems, the most commonly used of which are cron tasks and systemd timers. The configuration methods and code examples of these two methods are introduced below.

1. Use cron task to configure scheduled tasks

  1. Edit cron task table
    Enter the following command on the command line to edit the current user's cron task table:
crontab -e

If you need to edit the system-wide cron task table, you can use the following command:

sudo crontab -e
  1. Write cron task
    In the open file, each line represents a scheduled task . The format of each line is as follows:
分钟 小时 日 月 周 要执行的命令

Among them, minutes, hours, days, months and weeks represent the time of task execution, and the wildcard character * can be used to represent any value. The command to be executed is an operation that needs to be performed regularly.

For example, the following is an example of a scheduled task that executes a script at 1 am every day:

0 1 * * * /path/to/script.sh
  1. Save the cron task
    After editing the cron task, press Ctrl quit. The system will automatically save the modified cron task list in the corresponding location.

Tip: If you need to delete a cron task, use the following command:

crontab -r

2. Use systemd timer to configure the scheduled task

  1. Create one service file

Create a file with the suffix .service in the /etc/systemd/system/ directory, such as mytask.service .

sudo nano /etc/systemd/system/mytask.service
  1. Write the content of the service file

In the created service file, add the following content:

[Unit]
Description=My Task

[Service]
ExecStart=/path/to/script.sh

[Install]
WantedBy=multi-user.target

Among them, ExecStart Indicates the script file to be executed, WantedBy=multi-user.target indicates that this task should be executed when the system starts.

  1. Create a timer file

Create a timer file named .timer in the /etc/systemd/system/ directory File with suffix, such as mytask.timer.

sudo nano /etc/systemd/system/mytask.timer
  1. Write the content of the timer file

In the created timer file, add the following content:

[Unit]
Description=Run My Task every day

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Among them, OnCalendar=daily means that this task is executed once a day, Persistent=true means that even if the system is shut down before the task execution time, the task will be executed at the next startup.

  1. Reload systemd configuration

Execute the following command to make systemd reload the configuration file:

sudo systemctl daemon-reload
  1. Start the scheduled task

Execute the following command to start the scheduled task:

sudo systemctl start mytask.timer
  1. Set the scheduled task to start automatically at boot

Execute the following command to set the scheduled task to start automatically at boot:

sudo systemctl enable mytask.timer

The above are the steps and code examples for configuring system scheduled tasks on a Linux system. By using cron tasks or systemd timers, we can easily perform required operations regularly and improve work efficiency and convenience of system management.

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

Statement:
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