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 crontab

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

Share

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

Editor to share with you how to use crontab, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Linux crontab is a command used to execute programs on a regular basis, and this task scheduling command is launched by default when the operating system is installed. The crond command checks regularly every minute to see if there is any work to be performed, and if there is any work to be performed, it will be performed automatically.

Crontab profile

Task scheduling under Linux is divided into two categories: system task scheduling and user task scheduling. Linux system tasks are controlled by the cron (crond) system service, which is started by default. Scheduled tasks set by the user themselves use the crontab command. In CentOS system

Cat / etc/crontab

The configuration file can be explained as follows:

SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/# For details see man 4 crontabs# Example of job definition:#.-minute (0-59) # |.-hour (0-23) # | |.-day of month (1-31) # |.-month (1-12) OR jan,feb,mar Apr. # |.-day of week (0-6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat# | # * user-name command to be executed

The first four lines are the environment variables used to configure the crond task to run. The first line of SHELL variable specifies which shell the system will use, here is the second line of bash;, the PATH variable specifies the path to which the system executes the command; the third line of MAILTO variable specifies that the task execution information of crond will be sent to the root user by email. If the value of the MAILTO variable is empty, the task execution information will not be sent to the user. The HOME variable on the fourth line specifies the home directory to be used when executing a command or script.

The work that users have to perform on a regular basis, such as backup of user data, scheduled email reminders, etc. Users can use the crontab tool to customize their scheduled tasks. All user-defined crontab files are saved in the / var/spool/cron directory. Its file name is the same as the user name.

Meaning of crontab file

In the crontab file created by the user, each line represents a task, and each field on each line represents a setting. Its format is divided into six fields. The first five paragraphs are the time setting section, and the sixth paragraph is the command segment to be executed. The format is as follows: minute hour day month week command

The following special characters can also be used in each of the above fields:

"" represents all numbers in the range of values, or 1 to 12 months if the month field is

"/" means every certain time interval, for example, the minute field is * / 10, which means that it is executed every 10 minutes.

"-" represents a range from a certain interval, which is a closed interval. For example, "2-5" means "2Magne3-4JO5", and 0-23pac2 in the hour field means that it is executed every 2 hours within the range of 0: 23.

Scattered numbers (not necessarily continuous), such as 1, 2, 3, 4, 4, 7, 9.

Note: because the first day of the week varies from place to place, Sunday=0 (first day) or Sunday=7 (last day).

Detailed explanation of crontab command

Format:

Crontab [- u user] filecrontab [- u user] [- I] {- e |-l |-r}-u user: used to set a user's crontab service; file: file is the command file name, indicating that file is used as a crontab task list file and loaded into crontab;-e: edit the contents of a user's crontab file, and indicate the current user if no user is specified -l: displays the contents of a user's crontab file, or represents the current user if no user is specified;-r: deletes a user's crontab file from the / var/spool/cron directory. -I: give a confirmation prompt when deleting the user's crontab file. Crontab Note there are two editing methods for crontab: directly edit / etc/crontab file and crontab-e, where the scheduled task in / etc/crontab is the scheduled task in the system, while the user's scheduled task needs to be edited through crontab-e Each time you edit a user's cron settings, cron automatically generates a file with the same name as the user under / var/spool/cron. The user's cron information is recorded in this file. This file cannot be edited directly, but can only be edited with crontab-e. Command in crontab uses absolute paths as much as possible, otherwise tasks will often fail to execute because of path errors. The newly created cron job will not be executed immediately. It will take at least 2 minutes to execute. It can be executed immediately from the cron. % means "line break" in the crontab file, so if the script or command contains%, you need to use% to escape. Crontab configuration instance

Execute command every minute (because cron scans every 1 minute by default, so it's all *)

* * command

Command is performed at the 3rd and 15th minutes of each hour

3Jing 15 * command

Command is performed daily at the 3rd and 15th minutes from 8 am to 11:00:

3Jing 15 8-11 * command

Command is performed every 2 days at the 3rd and 15th minutes from 8 am to 11:00:

3 command 15 8-11 * / 2 * *

Command is performed every Monday at the 3rd and 15th minutes from 8am to 11:00

3 command 15 8-11 * * 1

Restart smb at 21:30 every night

30 21 * / etc/init.d/smb restart

Restart smb at 4: 45 on the 1st, 10th and 22nd of each month

45-4-1-10-10-22 * / etc/init.d/smb restart

Restart smb at 1: 10 every Saturday and Sunday

10 1 * * 6 0 / etc/init.d/smb restart

Restart smb every 30 minutes between 18: 00 and 23: 00 every day

0pr 30 18-23 * / etc/init.d/smb restart

Restart smb every hour

* * / 1 * / etc/init.d/smb restart

Restart smb every other hour between 11:00 and 7 a.m.

* 23-7bat 1 * / etc/init.d/smb restart

Restart smb on the 4th of each month and 11:00 from Monday to Wednesday

0 11 4 * mon-wed / etc/init.d/smb restart

Execute scripts in the / etc/cron.hourly directory every hour

0 1 * root run-parts / etc/cron.hourly above is all the content of this article "how to use crontab". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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