Crontab timed task does not perform error summary in Linux

Source: Internet
Author: User
Tags manual

Failure to execute a shell script through a timed task in CentOS failed with the following shell script, which was not written according to Spec:

1) The execution of the shell script (Sh backup.sh) was executed successfully, excluding the syntax error of the SH script.
2 through the Nano/etc/crontab command to view the timing of the task, found that in addition to the timing of the task of SH, other tasks can be carried out normally. Check its code,

found that the shell, PATH, MAILTO, home has not been well understood, the past is to pay attention to the following time rules, so check the definition:
1 shell, the value of the variable specifies the shell environment (here defaults to bash shell);
2 path, the variable defines the path of the program used to execute the command;
3) MAILTO, the output of the task is mailed to the user name of the MAILTO variable definition, and if the variable is defined as a blank string, the email will not be sent;
4 Home, variables can be used to set the master directory used when executing commands or scripts;

Then see the definition of the shell first with a env look at the system default shell is which address,


[Root@ay data]# Env
Hostname=ay
Term=xterm
Shell=/bin/bash
...
Then you open the shell script and see that there is no address configured for the shell script. OK, here's a question, here's a description of the shell address at the beginning of the shell script, like my env is shell=/bin/bash, so the shell script should start with the following:


#!/bin/bash
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
After adding, restart service crond Restart, check the script operation, smooth implementation ~

This time it was an exclusion check for the cause of the error, mainly due to the failure of the shell script written in accordance with the specification, and the following list of common problems that would occur if the shell was not written as spec:

1 because the startup script uses the system crontab, therefore must define the SH file the directory as well as the specific log specific path
2 Note Use Add and subtract ((num= $line + 1)) Two parentheses for weak type conversion
3) Note If the size of the if is used [[]] For example: if [[$count > 0]], equal to the use of one []

Problem Solving Ideas:


1. To determine if crontab has been executed, you can add a small script that executes every minute of the write file to test, if there is a record stating that crontab itself service is no problem.

such as: Feb 7 14:45:01 1280859075761a8z crond[13638]: (Root) CMD (/backuptoqiniu/backuptoqiniu.sh) This should mean that the crontab has been successfully executed, So crontab service is no problem.
2. In fact, most of the experience will know that the implementation of crontab is not related to the environment variables, the solution is to log in the script, in addition to the default of all the commands using the full path of the way.

Debug method: Add the log file, all the original echo can try to append to the log file, or when crontab write to append the log.
* * */backuptoqiniu/backuptoqiniu.sh >>/tmp/out.log 2>&1

Ases.sql "-u $MYSQL _user-h $MYSQL _server-p$mysql_pass $DATEBASE >" $NOW-datab
I don't know what you are, not your own commands are not in the default system commands.

Test method: The path to test crontab is not the same as the path that is executed manually.

You can add an echo $PATH >/tmp/1.log in the crontab script
Contrast and perform the echo $PATH under your manual terminal

Method Three, see one more example

Crontab timed tasks do not execute the reason 1, the script syntax error in the crontab script is not timed to execute, first need to check the syntax of the script there is no problem.

2. Environment variable problems Sometimes we create a crontab, but the task does not automate, and manual execution of this task is not a problem, typically because the environment variable is not configured in the crontab file. We perform tasks manually in the current shell environment, the program can find the environment variables, and the system automatically perform task scheduling, does not load any environment variables. Therefore, we need to provide all the necessary path and environment variables in the shelll script.

The main points to be noted are the following three:

1 Write the global path when the file path is involved in the script;

2 when the script executes to use Java or other environment variables, the source command is used to introduce environment variables, such as:

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 is executed manually OK, but crontab is not executed. At this time must be bold suspicion of environmental variables, and can try to directly introduce environmental variables in the crontab to solve the problem. Such as:


0 * * * *. /etc/profile;/bin/sh/var/www/java/audit_no_count/bin/restart_audit.sh

3, the system task scheduling and User task scheduling system task scheduling mainly completes some of the system maintenance operations, user task scheduling mainly completes user-defined tasks, you can put user task scheduling to the system task scheduling to complete (not recommended), but the reverse is not, The root user's task scheduling operation can be set through "crontab–uroot–e", or the dispatch task can be written directly to/etc/crontab file, it should be noted that if you want to define a task that periodically restarts the system, you must place the task in the/etc/ crontab files, even the task of creating a timed reboot system under root is not valid.

Crontab Scheduled Tasks do not execute the solution 1, view crontab execution record if the crontab timed task does not execute, first need to locate the problem, then need to pass the log to determine the problem.

The crontab log location is generally located in/var/log/cron, and the following statement allows you to view the log.

Tail-f/var/log/cron

The above/var/log/cron only records whether some of the planned scripts have been executed, but the execution is correct and some information about the execution of the script Linux is sent to the user by mail.
For the root user this message record is located in/var/spool/mail/root, the following command allows you to view the most recent crontab execution.

Tail-f/var/spool/mail/root

Mail messages typically only record the success of a script execution, and if execution fails to give further error information, we need to redirect the error message from the statement to the file so that you can easily view the error message. A simple example is given below.

0 6 * * */root/script/ss.sh >>/root/for_crontab/mylog.log 2>&1

The above statement indicates that both the error output and the standard output are exported to MyLog.log, and the information about the execution of the command is recorded in the MyLog.log file at execution time.

2, solve the problem after the previous step of the problem positioning, you can easily find the problem and solve ...

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.