Statement: This article reproduced from: http://www.taoyutaole.com/thread-1436-1-3.html
How to Use crontab in Linux, and how to use crontab.
As a website O & M engineer, you often have to deal with crontab. You can use crontab to automatically run scheduled tasks to achieve automated O & M. In my daily work, I also deeply felt the convenience and efficiency of crontab.
However, recently, during the use of crontab, there were successive headaches: there was no problem at all when manually executing the script, but the script was not executed in crontab. The final cause is that the file path in the script is not fully written, and the other is that the script needs to depend on the Java environment variable. In fact, both are caused by environment variables. I also learned from the Internet that crontab cannot run normally because environment variables are not necessarily identifiable in crontab. Some predecessors concluded that crontab will have problems sooner or later if you don't pay attention to it. Once this problem occurs, you will always remember it because it is a tough problem. Brilliant ~~
Next I will try to summarize the basic usage of crontab, environment variables, and other issues to be aware. If you have any questions, you can leave a message to discuss them.
----------------------------
1. Basic crontab usage
Usage: crontab [-u user] [-E |-L |-R]
The crontab format is described as follows:
* Comma (',') specifies the list value. For example, "1, 3, 4, 7, 8 ″
* The range value specified by the hyphen ('-') is "1-6", indicating "1, 2, 4, 5, 6 ″
* Asterisk ('*') indicates all possible values.
*/15 indicates execution every 15 minutes
# Use the hash sign to prefix a comment
# + ------ Minute (0-59)
# | + ----- Hour (0-23)
# | + ---- Day of month (1-31)
# | + --- Month (1-12)
# | + -- Day of week (0-7) (Sunday = 0 or 7)
# |
# ***** Command to be executed
2. crontab and Environment Variables
Do not assume that cron knows the required special environment. Therefore, make sure that all necessary paths and environment variables are provided in the shelll script, except for some automatically set global variables. Note the following three points:
1) write the global path when the script involves a file path;
2) When Java or other environment variables are used for script execution, use the source command to introduce the environment variables, such:
Cat start_cbp.sh
#! /Bin/sh
Source/etc/profile
Export run_conf =/home/d139/CONF/platform/white/cbp_jboss.conf
/Usr/local/jboss-4.0.5/bin/run. Sh-c pem &
3) When you manually execute the script OK, but crontab does not run. In this case, you must boldly doubt that it is caused by environment variables and try to introduce environment variables in crontab to solve the problem. For example:
0 *****./etc/profile;/bin/sh/var/www/Java/audit_no_count/bin/restart_audit.sh
3. Other issues that should be noted
1) the newly created cron job will not be executed immediately. It takes at least two minutes to execute it. If you restart Cron, run the command immediately.
2) After each job is executed, the system automatically sends an email to the current system user. It has been a lot of days and may even crash the entire system. Therefore, it is necessary to perform redirection after each job command:>/dev/null 2> & 1. The premise is that the commands in the job need to be properly output, such as appending to a specific log file.
3) when the crontab suddenly fails, try/etc/init. d/crond restart to solve the problem. Or check the log to see whether a job has been executed or whether tail-F/var/log/cron has been reported.
4) do not run crontab-R in disorder. It deletes your crontab file from the crontab directory (/var/spool/cron. All crontabs deleted from the user are gone.
5) in crontab, % indicates a line break. If you want to use it, you must escape \ %. For example, the commonly used date '+ % Y % m % d' will not be executed in crontab, change to date '+ \ % Y \ % m \ % d ''.
------------------------------
/Etc/profile,/etc/bashrc,. bash_profile and. bashrc
"/Etc/profile", "~ /. Bash_profile and other configuration files will be automatically executed. The execution process is as follows: when logging on to the Linux system, first start "/etc/profile", and then start "~" in the user directory "~ /. Bash_profile ", if "~ /. Bash_login "and "~ /. Profile "is also executed when the file exists "~ /. Bash_profile "is called in sequence.
------
Cat. bash_profile
#. Bash_profile
# Get the aliases and functions
If [-f ~ /. Bashrc]; then
.~ /. Bashrc
Fi
# User specific environment and startup programs
Path = $ pathhome/bin
Export path
Alias Vi = Vim
You can see through the above script ~ /. Bash_profile file is called first ~ /. Bashrc, and then load the path.
------
The following are some differences:
/Etc/profile: This file sets the environment information for each user in the system. When the user logs on for the first time, this file is executed and starts from/etc/profile. d. Collect shell settings in the setting file of the directory;
/Etc/bashrc: execute this file for every user running bash shell. When bash shell is opened, the file is read;
~ /. Bash_profile: each user can use this file to input the shell information dedicated to their own use. When a user logs on, this file is only executed once! By default, it sets some environment variables to execute the user's. bashrc file,
~ /. Bashrc: This file contains bash information dedicated to your bash shell. This file is read when you log on and every time you open a new shell;
~ /. Bash_logout: execute this file every time you exit the system (exit bash shell;
---
/Etc/profile is a global function, in which the set variables act on all users ,~ The variables set in/. bash_profile can inherit the variables in/etc/profile and act on users.
~ /. Bash_profile is interactive and login to run Bash ;~ /. Bashrc enters bash in interactive non-login mode.