Web server process running in the background needs to be closed

When working with web servers (such as Tomcat, Apache, etc.) their processes are usually started in the background and cannot be easily closed by Ctrl+C or the Close-Button in GUIs. This goes for both the development process on a developer's workstation, as well as directly on the server in test or production. In Windows based systems those processes can be easily found in the process manager that can be accessed via the GUI. In Unix based systems (Linux, MacOSX) such a GUI doesn't always exist (especially on the server) or is sometimes hard to find. We need a console based solution to ... <ul> <li>Find the right web server process that is running in the background</li> <li>Terminate it via the console</li> </ul>
1 answer

Kill running process in a Unix-based console with the example of the Java Web Server Grizzly

1. Step: We need to find the proper running process in the background, for that we will use the command...


ps aux

... which lists all running processes which mostly is too long of a list:


jurgen 39191 0.0 0.8 904472 69368 ?? S 2:19PM 0:11.78 /Applications/Google Chrome
jurgen 39043 0.0 0.2 2524472 13976 ?? Ss 9:51AM 0:02.62 com.apple.security.pboxd
jurgen 38473 0.0 0.0 2467260 432 ?? Ss Tue03PM 0:00.20 com.apple.XType.FontHelper
...

So we filter out only the process that we are looking for, by using the command 'grep' and using the console pipe '|' to concatenate the commands


ps aux | grep java

Result:

jurgen 39543 0.1 1.9 4845420 163316 s008 S+ 5:49PM 0:07.18 /Library/Java/JavaVirtu... exec:java
jurgen 39573 0.0 0.0 2432768 460 s007 S+ 6:08PM 0:00.00 grep java

Now this returns us the proper process but also the 'grep' process itself which we were just running in our pipe, to filter this, we concatenate yet another 'grep -v grep':


ps aux | grep java | grep -v grep

... and we only get the process we need

jurgen 39543 0.1 1.9 4845420 163316 s008 S+ 5:49PM 0:07.18 /Library/Java/JavaVirtu... exec:java

Now that we have identified our process with its process number 39543 (second column in our process), we can terminate it with the 'kill -9'* command, as follows:


kill -9 39543

* If your wondering: The -9 parameter tells the kill-Command to send a SIGKILL signal to our process

In conclusion the following - more generic code - needs to be executed:

ps aux | grep [server_process_name] | grep -v grep
kill -9 [identified_process_id]

Taggings: