Cause
: During the deployment of Java programs over the past few days, I found that every time I restart the Java app, I have to first Ps out the process ID and then kill it to kill the process, however, if the server only deploys one or two Java programs, this operation is acceptable. However, if there are several or dozens of Java programs on the server, this is depressing, in addition, when you start the Java program, use-CP to load the jar library file. a ps will show a lot of Java and jar ..... Then, grep is still dazzled.
Find a solution
: In Linux, the PID file is sometimes seen. To put it bluntly, it is the file generated after the program is started to identify the process ID. Therefore, you have created your own PID file, in this case, can Java programs be effectively divided? If you have an idea, you can implement it, or this is a feature of a programmer.
Attempt 1 (failed)
:
At first, I thought that the Java program was started by Bash. I checked whether the command was executed and then returned the PID command. I found the following method:
#! /Bin/bash
PID = 'echo "test PID vange "'
Echo $ PID
But after the test, I found that it is not very OK. This command is added with 'only the character after the command is returned. If you want to use this method, you need to modify your Java code, that is, after the Java program is executed, let JAVA print its own PID Number (this is to analyze the Java XXX identifier (forgot), but also to process these characters, the most terrible is not universal) and then no content is output. I believe that not many developers are willing to modify code hard for better management. This solution can only be rejected.
Attempt 2 (failed)
:
The first solution won't work. Continue to find a solution and look at the basic bash variables. You can try one of them:
#! /Bin/bash
PID = 'echo "test PID vange "'
Echo $!
This $! Is the result of the last command run. If it is correct, 0 is returned .... So this solution was rejected.
Attempt 3 (successful)
:
I learned from the code of/etc/INI. d/ntpd, and finally found a breakthrough: start-stop-daemon (that's right, it's you)
Start-stop-daemon is a command dedicated to creating processes in the system background and stored in/sbin/start-stop-daemon (Debian, however, its permissions can be used by general users, and it does not seem to have any permission vulnerabilities. However, it is convenient for general users to copy data to the/bin directory.
CP/sbin/start-stop-daemon/bin
Currently, vange can be used by common users. The following is a test example:
#! /Bin/bash
Start-stop-daemon -- start -- quiet -- pidfile/home/vange/XX. PID-M -- exec/bin/tailf --/home/vange/log
The command format is simple (or not simple). There are two main points to note:
1. -- this symbol in the command indicates that the parameters following the command are passed directly to the Command (-- exec part) without any processing)
2. The-M (-- make-pidfile) parameter indicates automatic creation (because this parameter is almost considered unfeasible)
Run the command, and then you can view and open another terminal, PS the command we just run (it is always there)
PS-Ef | grep tail
Vange 3036 2029 0 00:00:00 pts/0/bin/tailf/home/vange/log
Vange 3050 2390 0 00:00:00 pts/4 grep tail
The PID is 3036.
Check the PID file we created:
CAT/home/vange/XX. PID
3036
That's all we need!
The example is here. Now we can implement Java program management based on this. You don't have to worry about killing others.
For how to stop this program, see the start-stop-daemon command parameters!
Note: To stop a program, you must add the-P (-- pidfile) parameter. If it is not added, it will be terrible !! It isKillall
This is not a joke! Once all Java is disabled, you don't know which one to start to remedy it. It's too late, hurry up and find the operations staff (if there is no operation, you can only find the boss to reflect the situation (if the boss is not clear, then wait for the situation to be scolded !))
==========================================
After finding the materials for an hour and adding the words worth more than half an hour, it seems that it is not easy to publish an article,
I hope an article will reduce the time you need to repeat the materials and help you manage Java programs.
Address: http://blog.csdn.net/Vange/archive/2010/07/22/5756470.aspx
==========================================