Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Crontab environment variable problem

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Run the script in crontab to view the information related to hadoop. Because hadoop and java are deployed under / usr/local by default, an error occurred during the execution of crontab. It has something to do with environmental variables, which is described more clearly below.

#

The following sources:

Http://xiachaofeng.iteye.com/blog/1405184

I encountered a problem today. Crontab's scheduled tasks can't be executed automatically, but manual script execution has always been successful. At the end of the check, it was found that the system environment variables were used in the script. Let's begin to explain:

1.crontab and Environmental variables

Don't assume that cron knows the special environment you need, but it doesn't. So make sure you provide all the necessary paths and environment variables in the shelll script, except for some automatically set global variables. So pay attention to the following three points:

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

two。 Other problems that should be paid attention to

1) the newly created cron job will not be executed immediately. It will take at least 2 minutes to execute. If you restart cron, it will be executed immediately.

2) after the execution of each JOB, the system will automatically send the output to the current system user. Over time, there are so many that it may even burst the whole system. So it is necessary to redirect after each JOB command: > / dev/null 2 > & 1. The premise is that some processing has been done to the normal output of commands in Job, such as appending to a specific log file.

3) when crontab suddenly fails, you can try / etc/init.d/crond restart to solve the problem. Or check the log to see if a job has executed / reported an error tail-f / var/log/cron.

4) never run crontab-r indiscriminately. It deletes the user's Crontab file from the Crontab directory (/ var/spool/cron). Deleted all the crontab of the user is gone.

5)% has a special meaning in crontab, indicating the meaning of line wrapping. If you want to use it, you must escape\%. For example, the frequently used date'+% Y% m% d' will not be executed in crontab and should be replaced with date'+\% Y\% m\% d``.

Output configuration in 3.rontab

Crontab often configures the output of the run script as: > / dev/null 2 > & 1 to avoid content output in the crontab run.

The output of the shell command can be defined in the form of'>'.

/ dev/null stands for empty device file

> indicates where to redirect, for example: echo "123" > / home/123.txt

1 represents stdout standard output, and the system default value is 1, so "> / dev/null" equals "1 > / dev/null"

2 indicates stderr standard error

& indicates the equivalent meaning, 2 > & 1, indicating that the output redirection of 2 is equivalent to 1

So the meaning of the redirect output statement:

1 > / dev/null first indicates that standard output is redirected to an empty device file, that is, no information is output to the terminal and no information is displayed.

2 > & 1 means that the standard error output redirection is equivalent to the standard output, because the standard output has been redirected to the empty device file, so the standard error output is also redirected to the empty device file.

# # #

Troubleshooting found that there is no export / usr/local path in / etc/profile. So the path for hadoop and java can only be manually export to the script, or it can be assigned to the variable:

Export JAVA_HOME= "/ usr/local/jdk1.6.0_45"

Hadoop='/usr/local/hadoop-2.4.0/bin/hadoop'

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report