Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use cron to schedule tasks

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use cron to arrange tasks". In daily operation, I believe many people have doubts about how to use cron to arrange tasks. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use cron to arrange tasks". Next, please follow the editor to study!

Crontab file

Crontab, or "cron table", is a simple text file that contains rules and commands that specify the interval between the execution of tasks. Crontab files fall into two categories:

1) system-wide crontab files

These are typically used by Linux services and critical applications that require root privileges. The system crontab file is located in / etc/crontab and can only be accessed and edited by root users. It is commonly used to configure system-wide daemons. The crontab file looks like this:

Etc-crontab-linuxetc-crontab-linux

2) crontab files created by users

Linux users can also create their own cron tasks with the help of the crontab command. The cron tasks you create will run as the user who created them.

All cron tasks are stored in / var/spool/cron (for RHEL and CentOS distributions) and / var/spool/cron/crontabs (for Debian and Ubuntu distributions), and the cron task is listed with the user name of the user who created the file.

The cron daemon silently checks the / etc/crontab file and / var/spool/cron and / etc/cron.d*/ directories in the background.

The crontab command is used to edit the cron file. Let's look at the structure of the crontab file.

Analysis of crontab files

Before we continue, we need to explore the format of the crontab file. The basic syntax of the crontab file consists of five columns, represented by an asterisk, followed by the command to be executed.

* command

This format can also be expressed as follows:

M h d moy dow command

Or

M h d moy dow / path/to/script

Let's explain each entry.

M: stands for minutes. The range is 0 to 59

H: indicates hours, with a range of 0 to 23

D: represents a day of the month, ranging from 1 to 31

Moy: this is the month of the year. The range is 1 to 12.

Dow: what day is it? The range is 0 to 6, where 0 represents Sunday

Command: these are the commands to be executed, such as backup commands, restart and copy commands, etc.

Manage cron tasks

After looking at the structure of the crontab file, let's look at how to create, edit, and delete cron tasks.

Create a cron task

To create or edit a cron task as root, run the following command:

# crontab-e

To create or schedule a cron task for another user, use the following syntax:

# crontab-u username-e

For example, to run the cron task as Pradeep, issue the following command:

# crontab-u Pradeep-e

If the crontab file does not already exist, you will open a blank text file. If the crontab file already exists, the-e option allows you to edit the file

List crontab files

To view the cron task that has been created, simply pass the-l option:

# crontab-l Delete the crontab file

To delete a cron task, simply run crontab-e and delete the desired cron task line, and then save the file.

To delete all cron tasks, run the following command:

# crontab-r

Then, let's take a look at the different ways of arranging tasks.

Example of scheduling tasks using crontab

As shown in the figure, all cron task files have companion shebang headers.

#! / bin/bash

This indicates the shell you are using, in this case, bash shell.

Next, use the cron task entry we specified earlier to specify the time interval at which you want to schedule the task.

To restart the system at 12:30 every afternoon, use the following syntax:

30 12 * / sbin/reboot

To schedule a restart at 4:00, use the following syntax:

0 4 * / sbin/reboot

Note: the asterisk * is used to match all records.

To run the script twice a day (for example, 4:00 and 4:00), use the following syntax:

0 4jue 16 * / path/to/script

To schedule the cron task to run at 5:00 every Friday afternoon, use the following syntax:

0 17 * * Fri / path/to/script

Or

0 17 * 5 / path/to/script

If you want to run the cron task every 30 minutes, use:

* / 30 * / path/to/script

To schedule the cron task to run every 5 hours, run:

* * / 5 * / path/to/script

To run a script on a selected date (for example, 6:00 on Wednesday and Friday):

0 18 * * wed,fri / path/to/script

To run multiple commands using a single cron task, separate the tasks with semicolons, for example:

* / path/to/script1; / path/to/script2

Save time writing cron tasks with special strings some cron tasks can be easily configured with special strings corresponding to specific time intervals. For example,

1) @ hourly timestamp is equivalent to 0 *

It will perform a task at the first minute of each hour.

@ hourly / path/to/script

2) @ daily timestamp is equivalent to 0 0 *

It performs tasks at the first minute of each day (midnight). It can be useful in carrying out daily work.

@ daily / path/to/script

3) @ weekly timestamp is equivalent to 0 0 * 0

It performs cron tasks at the first minute of the week, and the first day of the week begins on Sunday.

@ weekly / path/to/script

3) @ monthly timestamp is equivalent to 0 01 * *

It performs tasks in the first minute of the first day of each month.

@ monthly / path/to/script

4) @ yearly timestamp is equivalent to 0 0 1 1 *

It performs tasks at the first minute of each year and can be used to send New year's greetings.

@ yearly / path/to/script restrict crontab

As a Linux user, you can control who has access to the crontab command. You can use the / etc/cron.deny and / etc/cron.allow files to control. By default, there is only one / etc/cron.deny file and does not contain any entries. To restrict users from using the crontab utility, simply add the user name to the file. When a user is added to the file and the user tries to run the crontab command, he or she will encounter the following error.

Restricted-cron-userrestricted-cron-user

To allow users to continue using the crontab utility, simply delete the user name from the / etc/cron.deny file.

If the / etc/cron.allow file exists, only the users listed in the file can access and use the crontab utility.

If neither file exists, only the root user has the privilege of using the crontab command.

Backup crontab entry

It is always recommended that you back up crontab entries. To do this, use the syntax:

# crontab-l > / path/to/file.txt

For example:

# crontab-l > / home/james/backup.txt check cron log

The cron log is stored in the / var/log/cron file. To view the cron log, run the following command:

# cat / var/log/cronview-cron-log-files-linuxview-cron-log-files-linux

To view the logs in real time, use the tail command, as follows:

# tail-f / var/log/cronview-live-cron-logsview-live-cron-logs at this point, the study on "how to use cron to arrange tasks" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report