The system of the company's server is CentOS6.4. you often need to use the Crontab command to execute a series of application scripts and encounter various problems, some problems encountered during the use of the Crontab command are summarized as follows for future use. Format Description: For the crontab command format, use a picture of a super-good person on the network to describe it: how to view logs when the crontab command is not as planned
The system of the company's server is CentOS 6.4. you often need to use the Crontab command to execute a series of application scripts and encounter various problems, some problems encountered during the use of the Crontab command are summarized as follows for future use.
Format Description
For the format of the crontab command, use a picture of a super-good person on the network to describe it:
How to View Logs
When the crontab command is not as planned, you can view the crontab log in linux to check the specific cause of the hang task. the specific method is to view the file/var/log/cron.
Problem summary
The following are some problems you encounter when configuring crontab:
1. problem description:
A script is written to the crontab scheduled task for execution. during the test, the shell command is run manually successfully, but the crontab scheduled task is not executed;
Solution:
This problem occurs because crontab execution is performed by users, and the import of environment variables is problematic. Generally, you need to write the absolute path to the script path and the path of the output log. at the same time, add the configuration file source ~ In The shell script ~ /. Bash_profile: import environment variables.
2. problem description:
The crontab execution script redirects the shell script output to the log file. the log file is named as Date. log, for example, 20150101.log.
You can write the crontab task as follows:
test.sh>>./`date -d last-day +%Y%m%d`.log >&1
3. problem description: write a script test. sh and manually execute the script test. sh> ./date -d last-day +%Y%m%d
. Log> & 1 is running, and logs are generated according to the date format. However, after this task is added to crontab, the task is not correctly executed;
* * * * * /app/test/dustbin/HY/test.sh>>/app/test/dustbin/HY/`date -d last-day +%Y%m%d`.log >&1
View the crontab log and find that the shell directory path is incomplete under the crontab task: Dec 9 11:26:01 host_name CROND [8066]: (test) CMD (/app/test/dustbin/HY/test. sh>/app/test/dustbin/HY/'date-d last-day +) solution: you need to modify the escape character of the preceding script. the modification is as follows:
* * * * * /app/test/dustbin/HY/test.sh>>/app/test/dustbin/HY/`date -d last-day +\%Y\%m\%d`.log >&1
After the modification, the crontab task is successfully executed.