In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to solve the errors of unexecuted crontab scheduled tasks in Linux. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Crontab is a scheduled task. However, after some netizens have configured crontab, they found that they did not perform the specified task the next day, so this problem has permissions, restrictions and other reasons. Let's summarize the solutions to the non-execution of crontab for you.
Failure to write the following shell script according to the specification results in failure to execute the shell script through the scheduled task in CentOS for troubleshooting:
1) Manual execution of shell script (sh backup.sh) was successfully executed to eliminate syntax errors in sh scripts.
2) check the scheduled tasks through the nano / etc/crontab command, and find that all the scheduled tasks can be executed normally except those that have been executed in sh. Check its code
I found that I didn't have a good understanding of SHELL, PATH, MAILTO and HOME. In the past, I always paid attention to the following time rules, so I checked the definition:
1) SHELL, the value of the variable specifies the shell environment (this defaults to bash shell)
2) PATH, the variable defines the program path used to execute the command
3) MAILTO, the output of the task is mailed to the user name defined by the MAILTO variable. If the variable is defined as a blank string, the email will not be sent.
4) HOME, variables can be used to set the home directory to be used when executing a command or script
Then you can see the definition of SHELL. First, use env to see which address is the default shell of the system.
[root@AY data] # env
HOSTNAME=AY
TERM=xterm
SHELL=/bin/bash
...
Then open the shell script to check, and sure enough, no address is configured for the shell script. OK, we have found a problem. According to the documentation, type the shell address at the beginning of the shell script. If my env is SHELL=/bin/bash, then the shell script should start as follows:
#! / bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
Restart the service service crond restart after adding, check the script, and execute it smoothly.
This time the cause of the error was checked by exclusion, mainly due to the failure of the shell script written in accordance with the specification.
Here are a few more common problems that occur when shell is not written according to the specification:
1) since the startup script uses the crontab of the system, the directory of the sh file and the specific path of the specific log must be defined.
2) pay attention to using addition and subtraction to use ((num=$line+1)) two parentheses for weak type conversion
3) Note that if judgment is much smaller than using [[]], for example: if [[$count > 0]], if it is equal to it, you can use a [].
Problem solving ideas:
1. To determine whether crontab has been executed, you can add a small script to write files executed every minute to test. If there is a record, there is nothing wrong with the crontab service itself.
For example, Feb 7 14:45:01 1280859075761a8Z crond [13638]: (root) CMD (/ backuptoqiniu/backuptoqiniu.sh) this should indicate that crontab has been successfully executed, so there is no problem with the crontab service.
two。 In fact, most people with experience will know that there are no relevant environment variables in the execution of crontab. The solution is to log in the script, and all commands will be full-path by default.
Debugging method: add log files, try to append all the original echo to the log file, or append the log when crontab writes.
45 14 * / backuptoqiniu/backuptoqiniu.sh > > / tmp/out.log 2 > & 1
Ases.sql "- u $MYSQL_USER-h $MYSQL_SERVER-p$MYSQL_PASS $DATEBASE >" $NOW-Datab "
I do not understand what this is, whether my own command is not in the default system command.
Test method: the PATH of testing crontab is different from the PATH executed manually.
You can add an echo $PATH > / tmp/1.log to the script of crontab
Compare with the echo $PATH executed under your manual terminal.
Method 3, look at another example
Reasons why crontab scheduled tasks are not executed 1. Script syntax errors when crontab scripts are not executed regularly, you first need to check whether there is anything wrong with the syntax of the script.
2. Environment variable problems sometimes we create a crontab, but this task cannot be performed automatically, but there is no problem with performing this task manually, which is usually caused by no configuration of environment variables in the crontab file. When we execute the task manually, we do it in the current shell environment, and the program can find the environment variables, but when the system automatically performs task scheduling, it will not load any environment variables. Therefore, we need to provide all the necessary paths and environment variables in the shelll script.
There are three main points to pay attention to:
1) write the global path when the file path is involved in the script
2) when java or other environment variables are used in script execution, introduce environment variables through the source command, 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 OK is executed manually, but crontab is not executed. At this point, you must boldly suspect that it is caused by the environment variable, and you can try to introduce the environment variable directly into the crontab to solve the problem. Such as:
0 *. / etc/profile;/bin/sh / var/www/java/audit_no_count/bin/restart_audit.sh
3. System task scheduling and user task scheduling system task scheduling mainly completes some maintenance operations of the system. User task scheduling mainly completes some user-defined tasks, which can be completed by putting user task scheduling into system task scheduling (not recommended), but not vice versa. Root users' task scheduling operation can be set through "crontab-uroot-e". You can also write the scheduling task directly to the / etc/crontab file. It is important to note that if you want to define a task to periodically restart the system, you must put the task into the / etc/crontab file, even if you create a scheduled restart task under the root user.
The solution to the non-execution of crontab scheduled tasks
1. Check the crontab execution record if the crontab scheduled task is not executed, you first need to locate the problem, then you need to use the log to determine where the problem lies.
The log location of crontab is generally located in / var/log/cron, and you can view the log using the following statement.
Tail-f / var/log/cron
The / var/log/cron above only records whether certain planned scripts are executed, but whether the specific execution is correct and some information during the execution of the script linux will be sent to the user in the form of email.
For root users, the mail record is located in / var/spool/mail/root, and you can view the recent crontab execution with the following command.
Tail-f / var/spool/mail/root
Mail mail generally only records whether the script execution is successful or not. If the execution fails, no further error message can be given. In this case, we need to redirect the error message executed by the statement to the file, so that we can easily view the error message. Here's a simple example.
0 6 * / root/script/ss.sh > > / root/for_crontab/mylog.log 2 > & 1
The above statement means that both the error output and standard output are output to mylog.log, and the relevant information about the execution of the command will be recorded in the mylog.log file during execution.
2. After the problem positioning in the previous step, the problem can be easily found and solved.
The above content is how to solve the error of unperformed crontab scheduled tasks in Linux. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.