Linux performs cron job failure, executes under shell SH but succeeds-environment variable?

Source: Internet
Author: User
Tags tmp file

Blog Category:
    • Linux
Linuxcrontabpermissionetc/profile Environment variables

One, we often encounter the execution of a command under the shell can be successful, such as executing a Java program:

Java-jar/home/opscoder/topo-audit.jar, but execution fails under crontab.

cornjob is 0 * * * Java-jar/home/opscoder/topo-audit.jar: 10 points per day to execute the main method in this jar

Second, to find the reason for the failure, that is, to see the execution log of Crotab, there are 3 ways, I use the Ubantu machine, using Method 1,Mail Task (Linux UNIX) in the/var/spool/mail/root file, There is a record of crontab execution log, tail-f/var/spool/mail/root can be used to view the recent crontab execution.

2. Query method for Uinx system (AIX, HP-UX)

In the/var/spool/cron/tmp file, there are croutXXX00999 tmp files that tail these files to see the tasks that are being performed.

3. Query method for Linux system (including Redhat, SUSE)

See/var/log/cron This file can be, can use tail-f/var/log/cron observation

Map to the system AIX root user default crontab

Third, for my situation, from the log can be seen as/bin/sh:java command not found

This shows that the environment variables used by Cron are not the environment variables of the system, but their environment variables, which is plainly, crontab cannot read the information under the/etc/profile.

Iv. Solutions:

1. Modify Crontab to invoke a script instead of executing the jar directly

Coming: Cornjob for 0 * * * java-jar/home/opscoder/topo-audit.jar

Modified to: 0 * * */home/opscoder/topo-check.sh

2. Create topo-check.sh

touchtopo-check.sh #创建topo-check.sh File
VI topo-check.sh #编辑topo-check.sh file and enter the following


#!/bin/bash
Source/etc/profile
Java-jar/home/opscoder/topo-audit.jar


Save exit.
chmod +x topo-check.sh #给topo-check.sh executable permissions

so you're done, you need to be aware of the need to add #!/bin/bash, or crontab may still fail to perform successfully, use one of the 3 methods above to view the log of crontab, and you will find something similar:/bin/sh command/home/opscoder/ topo-check.sh not found

attached: If the current user cannot start Crontab, you can start crontab (service cron status, service cron start) using a user with sudo privileges or with start rights (service) Service cron start), and then cut back to the user created or cron job (CRONTAB-E)

Linux performs cron job failure, executes under shell SH but succeeds-environment variable?

Related Article

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.