Linux (CentOS 6.5) Calls Java script and timed run script instance and configuration file specific explanation

Source: Internet
Author: User

Linux (CentOS 6.5) calls Java scripts and script instances that run on a timed basis
First, call Java program script (the Java environment is already built by default)

1, JDK installation path/usr/jdk/jdk1.7/...
2. Java Program path/USR/JDK
3. Class Name: Test.java (class name with main function)
4. Call the Java class script, note that the suffix of the script is. Sh
5. Script content:
(1) #!

/bin/bash
// #! Special symbols. This script is explained by the shell of Bash under the/bin folder and will explain the content to kernel to run
(2) CD/USR/JDK
(3)/usr/jdk/jdk1.7/bin/javac Test.java
(4)/usr/jdk/jdk1.7/bin/java Test
6. Script Name: testshell.sh
7. Run this script to compile the Java program, explaining the running JVM machine code

8. Run Script Command:sh testshell.sh


Second, scheduled to run the task

1, Linux has a system-level scheduled task service. Called Cron, the command in the terminal is crontab. Use him to run the script on a regular basis.

2, write the scheduled task of the file called Crontab in the/etc/folder

3. There are two ways of opening. One is User level, one is System level: CRONTAB-E User Level Vim crontab system level

4, there are two ways to open the way there are two ways to view. Crontab-l viewing user-level Scheduled Tasks More/less/tail/head viewing system-level tasks

5. Some properties defined in the Crontab file:

Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/
(1), the shell variable specifies which shell the system will use, and this is bash.

(2), the path variable specifies the paths of the system Run command.

(3), the mailto variable specifies that Crond's task run information will be emailed to the root user, assuming that the value of the mailto variable is null, it means that the task run information is not sent to the user.

(4), the home variable specifies the home folder to use when running the command or script.

6, the format of the scheduled task: an example of crontab, here is the use of vim editing, so it is system-level
# for details see Mans 4 Crontabs

# Example of Job definition:
#.----------------Minute (0-59)
# | .-------------Hour (0-23)
# |  | .----------Day of Month (1-31)
# |  |  | .-------month (1-12) OR jan,feb,mar,apr ...
# |  |  |  | .----Day of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat
# |  |  |  | |
# * * * * * * user-name command to be executed
* * * * * * Root sh/usr/jdk/testshell.sh

*/1 * * * * Root sh/usr/jdk/testshell.sh

Explanation: We can clearly see the format of the scheduled task through the crontab, so we don't have to say much. Both tasks defined above are run every minute of the task, and the user is root

The above is not a minute to run once:

The following is an hourly run:

0 * * * * rootsh/usr/jdk/testshell.sh//Hourly 0-minute run

0 */1 * * * rootsh/usr/jdk/testshell.sh//Hourly 0-minute run

Both of these methods define "0" minutes of each hour to run.

Of course, once in two hours, too:0 */2 * * * rootsh/usr/jdk/testshell.sh

* */2 * * * Rootsh/usr/jdk/testshell.sh This way there's no problem there's no problem note use

Run every few minutes within the specified time frame:

*/10 4-18 * * * root sh/usr/jdk/testshell.sh//Run every 10 minutes from 4 o'clock in the morning to 6 o'clock in the afternoon.

Run once per day at a specified time:

0 0 * * * rootsh/usr/jdk/testshell.sh//daily 0:0 run once

* * * Rootsh/usr/jdk/testshell.sh//daily 14:10 run once

Ten 10,11,14Rootsh/usr/jdk/testshell.sh//daily 10, 11, 14:10 run once

Run once a week with a specified date time:

0 0 * 0 Root sh/usr/jdk/testshell.sh //Run once every Sunday 0:0

* * 1Rootsh/usr/jdk/testshell.sh //Run once every Monday 19:1
15 *5 Rootsh/usr/jdk/testshell.sh//Run once every Friday 15:10

Note: 0 and 7 represent the Sunday

Specify a date time to run once a month:

0 0 1 * *Rootsh/usr/jdk/testshell.sh//0:0 on the 1th day of each month
the* *Rootsh/usr/jdk/testshell.sh//Every 20th day of the month, 14:15 run once

7, to here timed task is finished, but not necessarily will run successfully, the following say that rookie like me will often make mistakes

8, if the definition does not run the scheduled task. Then can go to/var/spool/mail to view the corresponding log, assuming that the error is the script to write a problem, note that there is a larger error probability is the path of the problem, so when writing a script can write the full path to try.

9, assuming no error, it is not running scheduled tasks. Cron is a timed run tool under Linux that can run jobs without human intervention. Because Cron is a built-in service for Linux, it does not take its own initiative to start and close the service in the following ways:

/sbin/service Crond Start//Startup service

/sbin/service Crond stop//Shut down service

/sbin/service crond Restart//Restart service

/sbin/service Crond Reload//load configuration again

10, you can also be able to use this service at the time of the system startup self-initiated:

At the end of the/etc/rc.d/rc.local script, add:

/sbin/service Crond Start


11, the time setting:

Minutes (0-59)

Hours (0-23)

Date (1-31)

Month (1-12)

Week (0-6)//0 on behalf of Sunday

In addition to the numbers there are a few special symbols that are "*", "/" and "-", ",". * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" represents from a number to a number, "," separate several discrete numbers


Third, in the Write call Java program script will have a-CP this is what to do that, then we look at:

1,-CP: Commentary
Because we write our own Java program to execute. You may want to include classes or class libraries other than java.lang.*. These class libraries or classes can be specified using-CP.
JAVA-CP C:\dir1\lib.jar Test
-CP is the same as-classpath. Is the path that specifies the other classes that the class executes on, typically the class library. Jar packages, such as the full path to the jar package. Window on the semicolon ";" While Linux goes Down is ":" with differentiated attention

2, The complete example shows:

Source/home/newsgroup/.bash_profile
Lang=zh_cn
Export LANG
Cd/www/autopublish.news.fang.com/auto_publishnews/bin
/usr/java/jdk1.6.0_35/bin/java -CP.: /lib/commons-httpclient-3.1.jar:.. /lib/commons-logging.jar:.. /lib/jdom.jar:.. /lib/log4j-1.2.8.jar:.. /lib/sqljdbc4.jar

Com.fang.autopub/newsautopublishing


3, Case analysis:

(1),

How to use the Source command:
SOURCE FileName
Function: Reads and runs the commands in filename in the current bash environment .


Note: This command often uses the command "." To replace.
such as: source. BASH_RC and. The. BASH_RC is equivalent.
Note: The difference between the source command and the shell scripts is:
source runs the command in the current bash environment , and scripts starts a child shell to run the command . This assumes that the environment variable settings (or alias, and so on) are written into the scripts, only affect the child shell, can not change the current bash, so through the file (command line) environment variable settings, to use the source command.


(2),
Export aaa= "Hello"
Just load this variable into memory and not write to which file.

Just like the ifconfig eth0 192.168.1.100 up, the system loads the files on the disk without writing to the disk, but after logging off or restarting.
To turn him into a permanent need to add him to the boot-up script. /etc/profile,/ETC/BASHRC the equation "global". Self-loading after booting. All the users share some files.

Scripts such as. BASHRC,. Barsh_profile, and so on in each user's home folder are "local" and only valid for that user. This satisfies the different needs of each user.

(3),

-CP Specifies the detailed path where these packages are located

The following package name + class name consists of the complete path, but you will find. The package name and the class name are not used between "," but "/"



Four, error analysis:

The script may not succeed at one time, so we have to analyze the error. And the timing task in crontab is not running how to view that, in detail such as the following:

1, check the log, there is no time to run your written task. Log path:/var/log/cron This is the log of the scheduled task, after opening it is assumed that you can see your command in the log stating that the scheduled task is running. The error comes from the script you wrote

2, if not in the log file. It means that you are not running your scheduled task, and we are restarting the crond service:/etc/init.d/crond restart


Summary: The crontab under Linux will take the initiative to help us read the/etc/crontab routine work every minute. But for some reason or another Unix system, because crontab is read into memory, it may not run immediately after you have changed/etc/crontab. This time please start Crond again this service is/etc/init.d/crond.


Linux (CentOS 6.5) Calls Java script and timed run script instance and configuration file specific explanation

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.