How to Automatically Kill a Process Using Script? [Duplicate]

How to write a script that kills a java process?

From console it looks like

root@ubuntu-s-1vcpu-1gb-ams3-01:~# lsof -i tcp:8080
COMMAND  PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
java    9657 root   26u  IPv6 4694148      0t0  TCP *:http-alt (LISTEN)
root@ubuntu-s-1vcpu-1gb-ams3-01:~# kill 9657

How should it be implemented in .sh file?

0

Just use the killall command. That should work. Like this:

killall java

(Just in case you don't know how to create a executable .sh file (It's not quite clear): A .sh file should begin with a shebang, something like

#!/bin/sh

After that, you just write commands like you would in a terminal, separated into lines. Then you run

chmod +x <insert filename>

and are able to execute the .sh file just fine. killall should be no exception

2
Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest