In Linux, cron regularly executes Java programs.

Source: Internet
Author: User

Environment:

RedHat Linux
Jdk5.0

Function Description:

A Java program is executed at two o'clock every morning to read data from the Oracle database for backup.

Procedure:

1. First, package the Java program into a jar package named auto. Jar. Be sure to set the main-class of manifest. MF of the jar package,
Copy the jar package to the/usr/local/directory, and copy the jar package that the jar package depends on to the/usr/local/directory, for example, classes12.jar;
2. Create a shell script in the/usr/local/directory with the script name auto. Sh. The script content is as follows:

#! /Bin/bash
CD/usr/local/

/Usr/Java/jdk1.5/bin/Java-jar auto. jar-CP
/Usr/Java/jdk1.5/lib/tools. jar:/usr/Java/jdk1.5/lib/dt. jar:/usr/local/classes12.jar :.


Note:
/Usr/Java/jdk1.5/is the Java installation path
The-CP parameter must be added after the Java command. cron does not load the system environment variable during execution. If this parameter is not added, the program will not execute normally,
Even if the command line can be correctly executed, problems may occur during cron calls.

3. Run the crontab command and enter the following content:
00 02 ***/usr/local/auto. Sh

Note:
Run the/usr/local/auto. Sh script at two o'clock every morning. For the syntax format expression of crontab, refer to the relevant manual.


4. Restart the cron process:
/Etc/init. d/cron restart

After the above configuration, the/usr/local/auto. Sh script is executed at two o'clock every morning to back up the data.

 

Recently, crontab cannot be executed. The following reasons are preliminarily summarized:

First, the cause of the script: In most cases, we need to believe in science, believe in computers, not ghosts, or our scripts. This problem leads to the probability that crontab cannot be executed.
More than 70%. The crontab execution ended when the program was executed at a certain step, and I encountered an error connecting the database during the migration code. As a result, the website cannot be accessed and died.

Second, the execution environment problem. When we encounter the first situation, we can usually manually execute the program to put the problem in the cradle. Generally, the experts should not make the first mistake. The problem is when we
When the manual execution is successful but crontab cannot be executed, I encountered an execution environment problem, for example, the related path setting problem. Solution: Execute source at the beginning of the Code
/Home/user/. bash_profile

Third, the system time is incorrect. This is a common and hidden problem. Solution: Date-s ********

The fourth is whether our script has the executable permission. Ensure that the user executing the script has the permission to execute the file change.

Fifth, the crontab daemon is dead. This situation is rare, but it is not ruled out. It can be used when we cannot find other reasons. Solution: restart the process.

 

In addition, the following describes how to view the most modified time of crontab:

In the/var/spool/cron/directory, there will be n files whose names are named. As long as crontab is created, there will be files whose names are named, the last modification time of the file is the last modification time of the user's crontab. Just do it

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.