How to kill a process running on particular port in Linux?
Use the command
sudo netstat -plten |grep java
used grep java
as tomcat
uses java
as their processes.
It will show the list of processes with port number and process id
tcp6 0 0 :::8080 :::* LISTEN
1000 30070621 16085/java
the number before /java
is a process id. Now use kill
command to kill the process
kill -9 16085
-9
implies the process will be killed forcefully.
Kill command does the job but gives error
As @Bodo pointed out, there was more than one PIDs and only one of them got killed while the other showed error as it wasn't started by the same user.
I changed the grep command to filter the process started by the same user and then killed it, so it worked.
Update:
I used this command to successfully run my code and terminate the app as needed.
ps -u | grep service.py | grep -v grep | awk '{print $2}' | xargs kill -9
How to kill a process by its pid in linux
Instead of this:
proid= pidof $proceso
You probably meant this:
proid=$(pidof $proceso)
Even so,
the program might not get killed.
By default, kill PID
sends the TERM
signal to the specified process,
giving it a chance to shut down in an orderly manner,
for example clean up resources it's using.
The strongest signal to send a process to kill without graceful cleanup is KILL
, using kill -KILL PID
or kill -9 PID
.
I believe it's some kind of problem with the bash language (which I just started learning).
The original line you posted, proid= pidof $proceso
should raise an error,
and Bash would print an error message about it.
Debugging problems starts by reading and understanding the error messages the software is trying to tell you.
Kill Command in linux
Try
#!/bin/sh
/usr/local/bin/wrun 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat' &
pid=$!
sleep 5
kill $pid 2>/dev/null && echo "Killed command on time out"
UPDATE:
A working example (no special commands)
#!/bin/sh
set +x
ping -i 1 google.de &
pid=$!
echo $pid
sleep 5
echo $pid
kill $pid 2>/dev/null && echo "Killed command on time out"
Why does the kill command work differently in bash and zsh
kill
is a shell builtin for both zsh and bash, with different implementations and options on each. The zsh builtin does support the POSIX -l
option for listing signals, but not the GNU -L
extension.
You can always use /bin/kill
to run the freestanding program version if you desire. On OSes with a GNU runtime, that'll also support -L
.
Related Topics
Find Files in Multiple Directories Taken from List in a File
Bash Script - "Tar Czf ..." Command Ignore Parameters
Using Pipe in Linux Using Parent and Child Process
Python3 Mlpy Installation Error - 'Py_Initmodule3' Was Not Declared in This Scope
Diff (Gnu Diffutils) 3.6 Exclude Directory
Bash-Script Error 0=1: Command Not Found
Killing Linux Process by Piping the Id
How to Run a Cron Job at Every 5 Hour Interval
Sending Mail Body and Attachment Using Mailx - Linux
Reading Input from Keyboard with X64 Linux Syscalls (Assembly)
Syntax Error of ";; Unexpected" on Simple Init Script for Debian
Bash Leftpad String with Spaces Inside Variable
R Programming - Submitting Jobs on a Multiple Node Linux Cluster Using Pbs
Reading Complete Line in 'For' Loop with Spaces, Tabs with Multiple Input Files
Find Command Find Directories That Were Created After a Certain Date Under Linux/Cygwin