Home >Operation and Maintenance >Linux Operation and Maintenance >How to configure system scheduled tasks on Linux
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
crontab -e
If you need to edit the system-wide cron task table, you can use the following command:
sudo crontab -e
分钟 小时 日 月 周 要执行的命令
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
Tip: If you need to delete a cron task, use the following command:
crontab -r
2. Use systemd timer to configure the scheduled task
Create a file with the suffix .service
in the /etc/systemd/system/
directory, such as mytask.service
.
sudo nano /etc/systemd/system/mytask.service
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.
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
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.
Execute the following command to make systemd reload the configuration file:
sudo systemctl daemon-reload
Execute the following command to start the scheduled task:
sudo systemctl start mytask.timer
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!