In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "the usage of crontab command under linux". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of crontab command under linux".
A brief introduction to crond
Crond is a daemon used under linux to periodically execute certain tasks or wait for certain events to be handled. Similar to the scheduled tasks under windows, when the installation of the operating system is completed, this service tool is installed by default, and the crond process is automatically started. The crond process periodically checks whether there is a task to be executed every minute, and automatically executes the task if there is a task 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 periodically performs, 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:
The code is as follows:
[root@localhost ~] # cat / etc/crontab
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
[root@localhost ~] #
The first four lines are the environment variables used to configure the crond task to run. The first line SHELL variable specifies which shell the system will use, here is bash, the second line PATH variable specifies the path to which the system executes the command, and the third line 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 meaning expressed by the sixth to ninth lines will be described in detail in the next section. There's no more to say here. User task scheduling: tasks that users perform on a regular basis, such as user data backup, 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.
User rights file:
File:
The code is as follows:
/ etc/cron.deny
Description:
Users listed in this file are not allowed to use the crontab command
File:
The code is as follows:
/ etc/cron.allow
Description:
Users listed in this file are allowed to use the crontab command
File:
The code is as follows:
/ var/spool/cron/
Description:
The directory where all users' crontab files are stored, named by 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 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:
The code is as follows:
Minute hour day month week command
Where:
Minute: represents minutes and can be any integer from 0 to 59.
Hour: represents an hour, which can be any integer from 0 to 23.
Day: represents the date and can be any integer from 1 to 31.
Month: represents the month and can be any integer from 1 to 12.
Week: represents 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.
The following special characters can also be used in each of the above fields:
Asterisk (*): represents all possible values. For example, if the month field is an asterisk, it means that the command operation is performed every month after the constraints of other fields are met.
Comma (,): you can specify a list range with values separated by commas, for example, "1, 2, 5, 7, 8, 9"
Middle bar (-): you can use the middle bar between integers to represent a range of integers, for example, "2-6" means "2pens 3pr 4je 5pl 6"
Forward slash (/): the interval frequency of the time can be specified with a forward slash, for example, "0-23gam2" indicates that it is executed every two hours. At the same time, a forward slash can be used with an asterisk, such as * / 10, which, if used in the minute field, means that it is executed every ten minutes.
II. Crond services
Install crontab:
The code is as follows:
Yum install crontabs
Service operation description:
/ sbin/service crond start / / start the service
/ sbin/service crond stop / / shut down the service
/ sbin/service crond restart / / restart the service
/ sbin/service crond reload / / reload the configuration
View the status of the crontab service:
Service crond status
Start the crontab service manually:
Service crond start
To see if the crontab service is set to boot, execute the command: ntsysv
Join the boot and start automatically:
The code is as follows:
Chkconfig-level 35 crond on
Third, the detailed explanation of crontab command
1. Command format:
The code is as follows:
Crontab [- u user] file
Crontab [- u user] [- e |-l |-r]
2. Command function:
With the crontab command, we can execute specified system instructions or shell script scripts at regular intervals. The unit of time interval can be any combination of minutes, hours, days, months, weeks and above. This command is very suitable for periodic log analysis or data backup.
3. Command parameters:
-u user: the crontab service used to set a user. For example, "- u ixdba" means to set the crontab service for the ixdba user. This parameter is usually run by the root user.
File:file is the name of the command file, indicating that file is used as a task list file for crontab and loaded into crontab. If this file is not specified on the command line, the crontab command accepts commands typed on the standard input (keyboard) and loads them into crontab.
-e: edit the contents of a user's crontab file. If you do not specify a user, you edit the current user's crontab file.
-l: displays the crontab file contents of a user. If no user is specified, the crontab file contents of the current user are displayed.
-r: delete a user's crontab file from the / var/spool/cron directory. If no user is specified, the current user's crontab file is deleted by default.
-I: give a confirmation prompt when deleting the user's crontab file.
4. Common methods:
1)。 Create a new crontab file
Before considering submitting a crontab file to the cron process, one of the first things to do is to set the environment variable EDITOR. The cron process uses it to determine which editor to use to edit the crontab file. 9.9% of UNIX and LINUX users use vi, and if you do the same, edit the $HOME directory. Profile file, adding a line like this:
EDITOR=vi; export EDITOR
Then save and exit. You might as well create a file called cron, where is the user name, such as davecron. Add the following to the file.
The code is as follows:
# (put your own initials here) echo the date to the console every
# 15minutes between 6pm and 6am
015 date' 30 45 18-06 * / bin/echo 'date' > / dev/console
Save and exit. Make sure that the first five fields are separated by spaces.
In the above example, the system will output the current time to the console every 15 minutes. If the system crashes or hangs, you can see at a glance when the system stopped working from the last displayed time. In some systems, the console is represented by tty1, and the above example can be modified according to the actual situation. To submit the crontab file you just created, you can use the newly created file as an argument to the cron command:
The code is as follows:
$crontab davecron
Now that the file has been submitted to the cron process, it will run every 15 minutes.
At the same time, a copy of the newly created file has been placed in the / var/spool/cron directory, and the file name is the user name (that is, dave).
2)。 List crontab files
To list crontab files, you can use:
The code is as follows:
$crontab-l
015 dev/tty1 15 30pm 45 18-06 * / bin/echo `date` > dev/tty1
You will see something similar to the above. You can use this method to make a backup of the crontab file in the $H O M E directory:
The code is as follows:
$crontab-l > $HOME/mycron
In this way, once you accidentally delete the crontab file, you can restore it quickly using the method described in the previous section.
3)。 Edit the crontab file
If you want to add, delete, or edit entries in the crontab file, and the E D I TO R environment variable is set to v I, you can edit the crontab file with v I, the corresponding command is:
The code is as follows:
$crontab-e
You can modify the crontab file and exit as you would any other file with v I. If some entries are modified or new entries are added, c r o n performs the necessary integrity checks on the file when it is saved. If one of these fields has a value that is out of the allowed range, it will prompt you.
When we edit the crontab file, we may add new entries. For example, add the following:
The code is as follows:
# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month
30 3 1 core' 7 14 14 21 26 * * / bin/find-name "core'-exec rm {}\"
Now save and exit. It's best to put a comment on each entry in the crontab file so that you know its function, its running time, and more importantly, which user's job it is.
Now let's use the crontab-l command mentioned earlier to list all its information:
The code is as follows:
$crontab-l
# (crondave installed on Tue May 4 13:07:43 1999)
# DT:ech the date to the console every 30 minites
0meme 15je 30pr 45 18-06 * / bin/echo `date` > / dev/tty1
# DT:delete core files,at 3.30am on 1,7,14,21,26,26 days of each month
30 3 1 core' 7 14 14 21 26 * * / bin/find-name "core'-exec rm {}\"
4)。 Delete crontab Fil
To delete the crontab file, use:
The code is as follows:
$crontab-r
5)。 Recover lost crontab files
If you accidentally delete the crontab file, assuming you have a backup in your $H O M E directory, you can copy it to / var/spool/cron/, where is the user name. "if the copy cannot be completed due to permission issues, you can use:"
The code is as follows:
$crontab
Where is the file name of your copy in the $H O M E directory.
I suggest you keep a copy of this file in your $H O M E directory. I have had a similar experience when I mistakenly deleted the crontab file several times (because the r key is next to the right of the e key). This is why some system documentation recommends that instead of editing the crontab file directly, edit a copy of the file and resubmit the new file.
Some variants of crontab are a little weird, so be careful when using the crontab command. If any options are left out, crontab may open an empty file or appear to be an empty file. At this time, hit the delete key to exit, do not press, or you will lose the crontab file.
5. Use an example
Example 1: execute command every 1 minute
Command:
* command
Example 2: execution at the 3rd and 15th minutes of each hour
Command:
3Jing 15 * command
Example 3: executed at the 3rd and 15th minutes from 8 a.m. to 11:00
Command:
3Jing 15 8-11 * command
Example 4: execution of the 3rd and 15th minutes from 8am to 11:00 every two days
Command:
3 command 15 8-11 * / 2 * *
Example 5: execution of the 3rd and 15th minutes from 8am to 11:00 every Monday
Command:
3 command 15 8-11 * * 1
Example 6: restart smb at 21:30 every night
Command:
30 21 * / etc/init.d/smb restart
Example 7: restart smb at 4: 45 on the 1st, 10th and 22nd of each month
Command:
45-4-1-10-10-22 * / etc/init.d/smb restart
Example 8: restart smb at 1: 10 on Saturdays and Sundays
Command:
10 1 * * 6 0 / etc/init.d/smb restart
Example 9: restart smb every 30 minutes between 18: 00 and 23: 00 every day
Command:
0pr 30 18-23 * / etc/init.d/smb restart
Example 10: restart smb with pm every Saturday at 11: 00 p.m.
Command:
0 23 * * 6 / etc/init.d/smb restart
Example 11: restart smb every hour
Command:
* * / 1 * / etc/init.d/smb restart
Example 12: restart smb every other hour between 11:00 and 7am
Command:
* 23-7bat 1 * / etc/init.d/smb restart
Example 13: restart smb on the 4th of each month and 11:00 from Monday to Wednesday
Command:
0 11 4 * mon-wed / etc/init.d/smb restart
Example 14: restart smb at 4: 00 on January 1
Command:
0 4 1 jan * / etc/init.d/smb restart
Example 15: hourly execution of scripts in the / etc/cron.hourly directory
Command:
01 * root run-parts / etc/cron.hourly
Description:
Run-parts this parameter, if you remove this parameter, you can write the name of a script to run instead of the directory name
IV. Points for attention in use
1. Pay attention to the problem of environmental variables
Sometimes we create a crontab, but the task cannot be performed automatically, but there is no problem with performing the task manually, usually because there are no environment variables configured in the crontab file.
When defining multiple scheduled tasks in the crontab file, one problem that needs to be paid special attention to is the setting of environment variables, because when we execute a task manually, it is carried out in the current shell environment, and the program can of course find the environment variables, but when the system automatically executes task scheduling, it will not load any environment variables, so it is necessary to specify all the environment variables needed for the task to run in the crontab file. There is no problem when the system performs task scheduling.
Don't assume that cron knows the special environment you need, but it doesn't. So make sure you provide all the necessary paths and environment variables in the shelll script, except for some automatically set global variables. So pay attention to the following three points:
1) write the global path when the file path is involved in the script
2) when java or other environment variables are used in script execution, introduce environment variables through the source command, such as:
The code is as follows:
Cat start_cbp.sh
#! / bin/sh
Source / etc/profile
Export RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf
/ usr/local/jboss-4.0.5/bin/run.sh-c mev &
3) when the script OK is executed manually, but crontab is not executed. At this point, you must boldly suspect that it is caused by the environment variable, and you can try to introduce the environment variable directly into the crontab to solve the problem. Such as:
The code is as follows:
0 *. / etc/profile;/bin/sh / var/www/java/audit_no_count/bin/restart_audit.sh
two。 Pay attention to cleaning the mail logs of system users
When each task is scheduled and executed, the system will send the task output information to the current system user in the form of e-mail. Over time, the log information will be very large, which may affect the normal operation of the system. Therefore, it is very important to redirect each task.
For example, you can set the following form in the crontab file to ignore the log output:
0 * / 3 * / usr/local/apache2/apachectl restart > / dev/null 2 > & 1
"/ dev/null 2 > & 1" means that standard output is redirected to / dev/null, and then standard error is redirected to standard output. Because standard output has been redirected to / dev/null, standard error is also redirected to / dev/null, so the log output problem is solved.
3. System-level task scheduling and user-level task scheduling
System-level task scheduling mainly completes some maintenance operations of the system, and user-level task scheduling mainly completes some user-defined tasks. User-level task scheduling can be completed by system-level task scheduling (not recommended), but the reverse is not recommended. Root users' task scheduling operations can be set through "crontab-uroot-e", or scheduling tasks can be directly written to / etc/crontab file. It is important to note that if you want to define a task to periodically restart the system, you must put the task in the / etc/crontab file, even if the task of creating a scheduled restart system under the root user is invalid.
4. Other considerations
The newly created cron job will not be executed immediately, but will take at least 2 minutes to execute. If you restart cron, it will be executed immediately.
When crontab suddenly fails, you can try / etc/init.d/crond restart to solve the problem. Or check the log to see if a job has executed / reported an error tail-f / var/log/cron.
Never run crontab-r indiscriminately. It deletes the user's Crontab file from the Crontab directory (/ var/spool/cron). Deleted all the crontab of the user is gone.
In crontab,% has a special meaning, indicating the meaning of line break. If you want to use it, you must escape\%. For example, the frequently used date'+% Y% m% d' will not be executed in crontab and should be replaced by date'+% Y\% m\% d'.
Thank you for reading, the above is the content of "the usage of crontab command under linux". After the study of this article, I believe you have a deeper understanding of the usage of crontab command under linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.