TrumanWong

crontab

Submit and manage users' tasks that need to be performed periodically

Supplementary instructions

crontab command is used to submit and manage users' tasks that need to be executed periodically. It is similar to scheduled tasks under Windows. When the operating system is installed, this service tool will be installed by default and the crond process will be automatically started. The crond process regularly checks every minute whether there is a task to be executed. If there is a task to be executed, the task is automatically executed.

grammar

crontab(options)(parameters)

Options

-e: Edit the timer settings for this user;
-l: List the timer settings for this user;
-r: Delete the timer settings of this user;
-u<user name>: Specify the user name to set the timer.

Parameters

crontab file: Specify the crontab file containing the tasks to be executed.

Knowledge expansion

Task scheduling under Linux is divided into two categories: System task scheduling and User task scheduling.

System task scheduling: The work that the system performs periodically, such as writing cached data to the hard disk, log cleaning, etc. There is a crontab file in the /etc directory, which is the configuration file for system task scheduling.

The /etc/crontab file includes the following lines:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""HOME=/

#run-parts
51 * * * * root run-parts /etc/cron.hourly
24 7 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

The first four lines are used to configure the environment variables for running the crond task. The SHELL variable in the first line specifies which shell the system should use, here is bash. The PATH variable in the second line specifies the path for the system to execute the command. The MAILTO variable in the third line specifies The task execution information of crond will be sent to the root user via email. If the value of the MAILTO variable is empty, it means that the task execution information will not be sent to the user. The HOME variable in the fourth line specifies the homepage used when executing the command or script. Table of contents.

User task scheduling: Tasks that users need to perform regularly, such as user data backup, regular email reminders, etc. Users can use the crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the /var/spool/cron directory. The file name is consistent with the user name, and the user permissions file is as follows:

/etc/cron.deny Users listed in this file are not allowed to use the crontab command
/etc/cron.allow Users listed in this file are allowed to use the crontab command
/var/spool/cron/ The directory where all user crontab files are stored, named after the user name

The meaning of the crontab file: In the crontab file created by the user, each line represents a task, and each field in each line represents a setting. Its format is divided into six fields, and the first five segments are time setting segments. , the sixth paragraph is the command section to be executed, the format is as follows:

minute hour day month week command order: minute hour day month week

in:

  • minute: represents the minute, which can be any integer from 0 to 59.
  • hour: represents the hour, which can be any integer from 0 to 23.
  • day: represents the date, which can be any integer from 1 to 31.
  • month: Indicates the month, which can be any integer from 1 to 12.
  • week: Indicates the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.
  • command: The command to be executed can be a system command or a script file written by yourself.

In each of the above fields, you can also use the following special characters:

  • Asterisk (*): represents all possible values. For example, if the month field is an asterisk, it means that the command operation will be executed every month after the constraints of other fields are met.
  • Comma (,): You can specify a list range with comma-separated values, for example, "1,2,5,7,8,9"
  • Center bar (-): You can use a center bar between integers to represent an integer range, for example, "2-6" means "2,3,4,5,6"
  • Forward slash (/): You can use forward slash to specify the time interval frequency, for example, "0-23/2" means to execute every two hours. At the same time, forward slashes can be used together with asterisks, such as */10. If used in the minute field, it means that it will be executed every ten minutes.

crond service

/sbin/service crond start # Start the service
/sbin/service crond stop # Close the service
/sbin/service crond restart # Restart the service
/sbin/service crond reload # Reload configuration

Check the crontab service status:

servicecrond status

Start the crontab service manually:

servicecrondstart

Check whether the crontab service has been set to start at boot, execute the command:

ntsysv

Add automatic startup at boot:

chkconfig –level 35 crond on

Example

Execute command every 1 minute

* * * * * command

Executed at the 3rd and 15th minute of every hour

3,15 * * * * command

Executed at the 3rd and 15th minutes from 8am to 11am

3,15 8-11 * * * command

Executed every two days at the 3rd and 15th minutes from 8 a.m. to 11 a.m.

3,15 8-11 */2 * * command

Executed every Monday from 8am to 11am on the 3rd and 15th minutes

3,15 8-11 * * 1 command

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 every month

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

Restart smb every Saturday and Sunday at 1:10

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

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

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

Restart smb every Saturday at 11:00 pm

0 23 * * 6 /etc/init.d/smb restart

Restart smb every hour

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

Restart smb every hour between 11pm and 7am

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

Restart smb on the 4th of every month and every Monday to Wednesday at 11 o'clock

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

Restart smb at 4 o'clock on January 1st

0 4 1 jan * /etc/init.d/smb restart

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

01 * * * * root run-parts /etc/cron.hourly