Crontab configuration and log usage records in CentOS

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.