How to use Cron to schedule cron tasks on Linux

WBOY
Release: 2023-07-05 14:49:06
Original
6879 people have browsed it

How to use Cron to schedule scheduled tasks on Linux

On Linux systems, we can use Cron to schedule scheduled tasks. Cron is a job scheduling tool that allows us to perform specific tasks regularly, such as backing up files, cleaning cache, etc. This article will introduce how to use Cron to schedule scheduled tasks on Linux and provide some code examples.

Step 1: Edit the Crontab file
Crontab (cron table) is a system-wide file that contains detailed information about all user scheduled tasks. We can use the command line tool provided by Cron to edit Crontab files.

Open a terminal window and enter the following command to edit the current user's Crontab file:

crontab -e
Copy after login
Copy after login
Copy after login

If it is the first time to edit the file, the system will ask you to choose an editor. We can choose the one you are familiar with. An editor such as nano or vim.

Step 2: Add a scheduled task
In the opened Crontab file, each line represents a scheduled task. The format of each line is divided into six fields: minute, hour, date, month, week, command. We can fill in these fields according to our needs.

The following is an example of a scheduled task, which performs a backup operation at 1 am every day:

0 1 * * * /usr/bin/backup_script.sh
Copy after login

Note:

  • The first field indicates minutes, range Is 0-59;
  • The second field represents the hour, the range is 0-23;
  • The third field represents the date, the range is 1-31;
  • The four fields represent the month, ranging from 1-12;
  • The fifth field represents the week, ranging from 0-7;
  • The last field is the actual command that needs to be executed.

If we need to perform tasks during a specific time period, we can use the wildcard "*/n" to replace the specific value. For example, the following example will execute a task every 10 minutes:

*/10 * * * * /usr/bin/task_script.sh
Copy after login

In the Crontab file, we can add multiple scheduled tasks, each task occupying one line.

Step 3: Save and exit
After editing is completed, we can use the shortcut key "Ctrl X" and then select "Y" to save the changes.

Step 4: View scheduled tasks
You can view the current user’s scheduled tasks list through the following command:

crontab -l
Copy after login

This will display all scheduled tasks for the current user.

Given that the system Crontab file is also used to schedule system-level tasks, we can use the following command to view the system-wide scheduled task list:

sudo cat /etc/crontab
Copy after login

In this file, you will find all users scheduled tasks.

Finally, to ensure that the Cron service has started, you can check its status using the following command:

sudo service cron status
Copy after login

Code Examples
Here, we provide two simple example codes to demonstrate how Use Cron to schedule scheduled tasks on Linux.

Example 1: Automatically send log files at 6 o'clock every morning

Create a file named "send_log.sh" and copy the following code into the file:

#!/bin/bash

# 这是一个模拟发送日志文件的脚本
echo "Sending log file: /var/log/app.log"
# actual send log command
Copy after login

Then, run the following command to edit the scheduled task:

crontab -e
Copy after login
Copy after login
Copy after login

Add the following lines to the opened Crontab file:

0 6 * * * /path/to/send_log.sh
Copy after login

Save and exit the file.

Example 2: Clean cache files every Friday at 3 pm

Create a file named "cleanup_cache.sh" and copy the following code into the file:

#!/bin/bash

# 这是一个模拟清理缓存文件的脚本
echo "Cleaning up cache files: /var/cache/*"
# actual cleanup command
Copy after login

Run the following command to edit the scheduled task:

crontab -e
Copy after login
Copy after login
Copy after login

Add the following line to the open Crontab file:

0 15 * * 5 /path/to/cleanup_cache.sh
Copy after login

Save and exit the file.

Summary
By using Cron, we can easily schedule scheduled tasks on the Linux system. First, we need to edit the Crontab file and then add the commands we want to execute regularly. We can use wildcards to set specific time intervals or add multiple scheduled tasks. Finally, we can use the command to view the current user or system-wide scheduled task list.

The above is the detailed content of How to use Cron to schedule cron tasks on Linux. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!