Crontab configuration and log usage records in CentOS

Source: Internet
Author: User

Crontab configuration and log usage records in CentOS

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.

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.