crontab prompt command not found solution

Source: Internet
Author: User

Meeting a problem today, Crontab's scheduled task will error: Java command not found, but the manual execution of the script has been successful.

Conjecture is the problem of environment variables.

To add a task for printing environment variables in crontab:

* * * * * echo $PATH

View Crontab Log/var/log/cron

You can see that there are only/usr/bin and/bin two directories in path.

No wonder we can't find Java.

Solution:

Add a line to the shell script

Source/etc/profile.

Another related article: http://xiachaofeng.iteye.com/blog/1405184

1.crontab and environment variables

Don't assume that Cron knows the special circumstances you need, and it doesn't really know. So you have to make sure that you provide all the necessary path and environment variables in the shelll script, except for some auto-set global variables. So note the following 3 points:

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

2) When script execution is used in Java or other environment variables, the environment variables are introduced 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 is executed manually, but crontab is not executed. At this point, we must boldly suspect that environmental variables are the bane, and can try to directly introduce environmental variables in crontab to solve the problem. Such as:
0 * * * *. /etc/profile;/bin/sh/var/www/java/audit_no_count/bin/restart_audit.sh

2. Other issues to be aware of

1) The newly created cron job will not execute immediately, at least 2 minutes. If you restart Cron, it will be executed immediately.
2) After each JOB execution, the system will automatically send the output mail to the current system user. It's very, very much, and it can even explode the whole system. So it is necessary to redirect after each JOB command: >/dev/null 2>&1. The prerequisite is that the commands in the Job require normal output to be processed, such as appending to a particular log file.
3) When the crontab suddenly fails, you can try/etc/init.d/crond restart solve the problem. Or check the log to see if a job has execution/error tail-f/var/log/cron.
4) Don't run crontab-r. It removes the user's crontab file from the crontab directory (/var/spool/cron). All crontab of the user have been deleted.
5) in Crontab, the% has a special meaning, indicating the meaning of the line break. If you want to use the words must be escaped \%, such as the frequently used date ' +%y%m%d ' in the crontab will not be executed, should be replaced by the date ' +\%y\%m\%d '.

Output configuration in 3.rontab

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

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

/dev/null represents an empty device file

Where does the > delegate redirect to, for example: echo "123" >/home/123.txt

1 means stdout standard output, the system default is 1, so ">/dev/null" is equivalent to "1>/dev/null"

2 indicates stderr standard error

& means equivalent to, 2>&1, 2 output redirect equals 1

Then the meaning of the redirected output statement:

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

2>&1 indicates that standard error output redirection is equivalent to standard output because the standard error output is redirected to an empty device file because the standard output was previously redirected to an empty device file.

crontab prompt command not found solution

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.