Running Java Applications in the Background
To execute a JAR file as a background process on Linux, utilize the nohup command combined with the ampersand symbol. This approach ensures the process continues running after terminal session termination.
nohup java -jar application-service.jar > runtime.log 2>&1 &
nohup: Maintains process execution after session treminationjava -jar application-service.jar: Executtes the speicfied JAR file> runtime.log: Redirects standard output to log file2>&1: Merges standard error with standard output&: Executes command in background mode
Monitoring Background Java Processes
To inspect currently running Java applications, employ process listing commands with appropriate filters:
ps aux | grep 'java.*jar'
For targeted process monitoring:
ps aux | grep 'specific-application.jar'
top -p [process-id]
Terminating Java Application Processes
To gracefully stop a running Java application:
- Identify the target process identifier:
ps -ef | grep target-application.jar
- Terminate using the acquired PID:
kill [process-id]
# For forced termination:
# kill -9 [process-id]
For automated process termination:
kill $(pgrep -f target-application.jar)
# Forceful alternative:
# pkill -f target-application.jar