Submit and manage users' tasks that need to be performed periodically
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.
crontab(options)(parameters)
-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.
crontab file: Specify the crontab file containing the tasks to be executed.
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:
In each of the above fields, you can also use the following special characters:
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
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