How to manage Java processes in Linux

Source: Internet
Author: User

This article describes how to manage Java processes in Linux. Due to the powerful PS commands in Linux, in Linux, it is not necessary to distinguish different Java processes as easily as in windows. However, in Linux, processes need to be managed like this: a commercial service system needs to provide 24-hour uninterrupted services, but for various reasons, this may cause the JVM crash corresponding to the current Java Process. Therefore, this article describes how to write a shell script to easily start, stop, and guard a Java Process.

First, the shell should have the start and stop functions. There are many methods to stop a process that I want to stop in Linux. the method I use is to record the process ID corresponding to the process to a file at startup, when the process is stopped, read the process ID from the file for kill. At the same time, make a crontab and keep searching for the existence of the process corresponding to the process ID in the file in the system. If it does not exist, restart the process.

Start and Stop scripts: Ctrl. Sh

Shell code
1 .#! /Bin/sh
2 .#
3. # start/stop the service
5 .#
6. # Do Some init here
7 .#
8. Case "$1" in
9. 'restart ')
11. # first stopping the service
12. PID = 'sed-N 1 p pidfile' # Get PID from File
13. If [! -Z "$ PID"]; then
14. Echo "Stopping the service, begin killing $ {pid }"
15. Kill $ {pid}>/dev/null 2> & 1
16. Sleep 2
17. fi
18. # second starting the service
19. If [some condition here]; then
20. Echo "starting the service"
21. Java-classpath some_class_path_here-jar helloworld. Jar &
22. Echo $! > Pidfile # record process ID to file
23. fi
24 .;;
25. 'stop ')
26. # Stopping the service
27. PID = 'sed-N 1 p pidfile' # Get PID from pidfile
28. If [! -Z "$ PID"]; then
29. Echo "Stopping the service, begin killing $ {pid }"
30. Kill $ {pid}>/dev/null 2> & 1
31. fi
32 .;;
33 .*)
34. Echo "unmarkable usage: $0 {restart | stop }"
35 .;;
36. esac
Then, run the crntb. Sh script to execute crontab.

Shell code
1 .#! /Bin/sh
2. PID = 'sed-N 1 p pidfile'
3. cmd = 'ps-E | grep $ PID '# Get process with the given PID
4. indx = 'expr Index "$ cmd" "Java" '# Whether the string 'cmd' contains 'java'
6. If ["$ indx" = "0"]; then
7./... path of ctrl. Sh.../ctrl. Sh restart
8. fi
Finally, execute the preceding crntb. Sh in crontab every minute.

Shell code
1. crontab-e

Shell code
1.0-59 *****/... path of crntb. Sh.../crntb. Sh
In this way, you can check whether the process corresponding to the current PID is still running every minute. If not, restart.
Of course, using these small pieces of code alone is not enough to maintain a complete commercial application. However, the first step of the long journey has been taken.

This article from the Linux community website ( original link:

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: 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.