A method that launches an executable. jar file and closes it with a shell script under Linux

Source: Internet
Author: User
Words do not say, start on the code, in order to lazy, the code is just a screenshot, interested friends can read after their own write, so deepen the impression.



Above is the shell script written by the younger brother. Please forgive me for not writing well.

First encountered the first pit

I wrote in the window and then the road Linux implementation, in Linux anyway said my 19 lines have errors, I do not understand, where there is a mistake, asked the next person, also said yes. Suddenly an expert asked me where to write, the younger brother answered in win, he told me to carry out this command:

Dos2unix server.sh

Then the younger brother executed, said the command does not exist, the younger brother had to install this, executed the following command

Yum Install Dos2unix

When he's done, the younger brother is executing Dos2unix server.sh.

And then in the execution. server.sh Start,

He could have been a miracle.

The younger brother asked what is the reason. Someone high man, silently answered the sentence, win under the line and Linux under different. Younger brother talents, intelligent silently to help the Niang.

Let me find this saying:

Linux is not the same as windows and line breaks. Under Windows is CRLF (\ r \ n or 0d0a), under Linux is LF (\ n or 0a). Under Linux, there are times when you encounter text files from Windows that have Windows line breaks, and there may be some inexplicable errors in scripting under Linux. Therefore, you need to convert these files to a Linux newline character.

Then someone else is using a different method. Specific links can be seen under the http://blog.chinaunix.net/uid-20726500-id-4492875.html

Then I was ecstatic, and finally I could.

However when I execute the Stop method there is a pit.

There's no way to do it, and the following things happen:


He actually said this ID did not find, the younger brother was silent, is he did not find to kill the ID of the process, there is this conjecture, the younger brother decisively executed the following command:


Little brother, at that time stunned, what is this command to carry out? Kill command is generally kill-9 (this is mandatory kill) PID AH this is not AH should be kill-9 1556, think of this I should get 1556 this PID, how to get it. Younger brother again please out of our great degree Niang, search for some, finally let me find this command:

Netstat-anp|grep 9997|awk ' {printf $} ' |cut-d/-f1, screenshot below


This value is what I want, decisively changed the order of the 7th line, changed to this. The younger brother is not, directly with vim to change


Then WQ to save, the following is to see the kill off it?

The server.sh Stop command is then executed, and the console outputs the


See this, should be kill off.

This is done.

So this gets the PID command, little brother still don't quite understand what is awk? Cut knows what it means.

Then this time, the magic of the mother. Many of the snooping, search, the younger brother found this

Awk is a powerful text analysis tool that is particularly powerful when it comes to analyzing and generating reports on data, compared to grep lookup and sed editing. To put it simply, awk reads the file one line at a time, using a space as the default delimiter to slice each row, cut the section, and then do a variety of analytical processing, the specific links please see below

Http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html

Through this link generally also understand

It has this explanation on it.

In addition, the $ variable refers to the entire record. Represents the first field in the current row, and $ $ represents the second field of the current row,...... Analogy

Little brother has this awk ' {printf $} '


So what does this $ mean, that is, to get the value of 1556/java, do not believe that their subordinates is not the seventh.

So since found this how to take 1556, if it is in Java, PHP, JS is easy, but under Linux, then you need cut command, cut as its name suggests, I do not explain,

He has a-D is a custom delimiter, so you can cut-d/

This means that the split in JS is almost the same meaning

And then I'll just take the first one.

Summed up is cut-d/-F 1

That's the value.



Execute jar file


Java class via SH script under Linux:

public class HelloWorld { 
    public static void Main (string[] args) { 
        system.out.println (" Hello world! "); &NBSP
    }  
}  

sh script:

#!/bin/ksh  
Echo-------- HelloWorld------->> " 
java_home="/usr/jdk/jdk1.6.0_34 " 
java_options="-hotspot-xms128m- Xmx512M " 
Class_path=. /lib; &NBSP
Java-jar Helloworld.jar  

Remarks:

in MANIFEST.MF in the jar package specify the class that runs the Main method:
Main-class: Com.test.DeveloperSybaseTest

If additional jar packages are required, specify the following parameters:
Class-path:./lib/jconnect2.jar
Note: class-path: You need a space, a space between multiple jar packages, and a line of characters that can't be too many, and you need to precede the jar pack with a space. In the MANIFEST.MF in the

Jar package, specify the class that runs the Main method:
Main-class:com.test.developersybasetest

If additional jar packs are required, specified by the following parameters: (Note: Class-path: After a space, multiple jar packages need a space, and a line of characters can not be too many, when changing lines need to precede the jar package with a space.)
Class-path:./lib/jconnect2.jar


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.