What killed my process and why?
If the user or sysadmin did not kill the program the kernel may have. The kernel would only kill a process under exceptional circumstances such as extreme resource starvation (think mem+swap exhaustion).
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.
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.
Killing a process in linux
Linux supports the BSD style switches to the ps
command (without the leading - ... dash/hyphen). If one supplies the hypen then the GNU coreutils version of ps
(the one which is standard on mainstream Linux distributions) will attempt to interpret the switches as SysV compatible. This is the source of your error.
I'd recommend using the BSD form of the switches and look up the -o
option to specify an output format consisting ONLY of the PID of the matching processes.
Also you're attempting to kill a zombie. As you've discovered that's a futile effort. A zombie is a placeholder in the process able for a process which is already dead. It remains in the process table until its parent process "reaps" its exit code. If the parent never does a wait()
system call then the entry will stay there until after the parent is killed, at which point the zombie (and any other orphaned processes) will be inherited by the init
process. The normal init
under Linux (or any other form of UNIX) periodically reaps all dead processes (zombies).
Conceptually every process that exits on a UNIX/Linux system spends a small amount of time as a "zombie" ... that is there should always be a period of time between the process' termination and the time when some other process reads its exit value (even if only to discard it, as init
does).
This question really should go on ServerFault
Find and kill a process in one line using bash and regex
In bash
, you should be able to do:
kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}')
Details on its workings are as follows:
- The
ps
gives you the list of all the processes. - The
grep
filters that based on your search string,[p]
is a trick to stop you picking up the actualgrep
process itself. - The
awk
just gives you the second field of each line, which is the PID. - The
$(x)
construct means to executex
then take its output and put it on the command line. The output of thatps
pipeline inside that construct above is the list of process IDs so you end up with a command likekill 1234 1122 7654
.
Here's a transcript showing it in action:
pax> sleep 3600 &
[1] 2225
pax> sleep 3600 &
[2] 2226
pax> sleep 3600 &
[3] 2227
pax> sleep 3600 &
[4] 2228
pax> sleep 3600 &
[5] 2229
pax> kill $(ps aux | grep '[s]leep' | awk '{print $2}')
[5]+ Terminated sleep 3600
[1] Terminated sleep 3600
[2] Terminated sleep 3600
[3]- Terminated sleep 3600
[4]+ Terminated sleep 3600
and you can see it terminating all the sleepers.
Explaining the grep '[p]ython csp_build.py'
bit in a bit more detail:
When you do sleep 3600 &
followed by ps -ef | grep sleep
, you tend to get two processes with sleep
in it, the sleep 3600
and the grep sleep
(because they both have sleep
in them, that's not rocket science).
However, ps -ef | grep '[s]leep'
won't create a process with sleep
in it, it instead creates grep '[s]leep'
and here's the tricky bit: the grep
doesn't find it because it's looking for the regular expression "any character from the character class [s]
(which is s
) followed by leep
.
In other words, it's looking for sleep
but the grep process is grep '[s]leep'
which doesn't have sleep
in it.
When I was shown this (by someone here on SO), I immediately started using it because
- it's one less process than adding
| grep -v grep
; and - it's elegant and sneaky, a rare combination :-)
How to kill all processes with a given partial name?
Use pkill -f
, which matches the pattern for any part of the command line
pkill -f my_pattern
Just in case it doesn't work, try to use this one as well:
pkill -9 -f my_pattern
How to kill a process on a port on ubuntu
You want to use backtick, not regular tick:
sudo kill -9 `sudo lsof -t -i:9001`
If that doesn't work, you could also use $()
for command interpolation:
sudo kill -9 $(sudo lsof -t -i:9001)
Related Topics
How to Redirect the Output of an Application in Background to /Dev/Null
How to Get the Current Network Interface Throughput Statistics on Linux/Unix
Best Distributed Filesystem for Commodity Linux Storage Farm
Shell Init Issue When Click Tab, What's Wrong with Getcwd
Graphical Diff Programs for Linux
How to Delete Multiple Files at Once in Bash on Linux
Colored Shell Script Output Library
Any Way to Exit Bash Script, But Not Quitting the Terminal
Excluding Directory When Creating a .Tar.Gz File
Custom Linux Gui: Where to Begin
Implementing an Update/Upgrade System for Embedded Linux Devices
How to Get Diff Between All Files Inside 2 Folders That Are on the Web
Symbolic Link to a Hook in Git
How to Recompile Just a Single Kernel Module
Why Can't You Use Cat to Read a File Line by Line Where Each Line Has Delimiters
How to Monitor Data on a Serial Port in Linux