Killing linux process by piping the id
Try the backtick operator for evaluating a sub-command
kill -s SIGINT `pgrep -f "python myscript.py"`
(untested)
How to give arguments to kill via pipe
kill $(ps -e | grep dmn | awk '{print $1}')
Kill process on the left side of a pipe
By killing the whole process group instead of just bash
(the parent), you can send the kill signal to all children as well.
Syntax examples are:
kill -SIGTERM -$!
kill -- -$!
Example:
bash -c 'sleep 50 | sleep 40' & sleep 1; kill -SIGTERM -$!; wait; ps -ef | grep -c sleep
[1] 14683
[1]+ Terminated bash -c 'sleep 50 | sleep 40'
1
Note that wait
here waits for bash to be effectively killed which takes some milliseconds.
Also note that the final result (1) is the 'grep sleep' itself. A result of 3 would show that this did not work as two additional sleep processes would still be running.
The kill
manual mentions:
-n
where n is larger than 1. All processes in process group n are signaled.
When an argument of the form '-n' is given, and it is meant to denote a
process group, either the signal must be specified first, or the argument
must be preceded by a '--' option, otherwise it will be taken as the signal
to send.
Shell script to capture Process ID and kill it if exist
Actually the easiest way to do that would be to pass kill arguments like below:
ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill
Hope it helps.
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 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 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
Kill process by pid file
I believe you are experiencing this because your default shell is dash (the debian almquist shell), but you are using bash syntax. You can specify bash in the shebang line with something like,
#!/usr/bin/env bash
Or, you could use the dash and bash compatible back-tick expression suggested by admdrew in the comments
kill -9 `cat /var/run/myProcess.pid`
Regardless, you can't rely on /bin/sh
to be bash.
What's the best way to send a signal to all members of a process group?
You don't say if the tree you want to kill is a single process group. (This is often the case if the tree is the result of forking from a server start or a shell command line.) You can discover process groups using GNU ps as follows:
ps x -o "%p %r %y %x %c "
If it is a process group you want to kill, just use the kill(1)
command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112
.
Related Topics
Using Sed Replace Line in File with Another File
Script Produces Different Result When Executed by Bash Than by Cron
Raspberry Pi Refusing Connection to Bottle Server
Undefined Reference to 'Pthread_Init' When Using -Lpthread Flag:
Serialport in Mono in Linux Not Responding to Datareceived Event
How to Not Emit Local Symbols in Nasm So That Gdb Disas Won't Stop at Them
Linux Equivalent of Windows Dll Forwarders or MACos Reexport_Library
How to Install Rpy2 via Conda Using Default R Installation
"Bad Interpreter" Error Message When Trying to Run Awk Executable
How to Properly Quote This Bash Pipeline for Watch
How to Start a Process That Won't End When My Ssh Session Ends
Shell Script Issue with Filenames Containing Spaces
How to Read Input from the Terminal Using /Dev/Stdin and Read.Csv()
Bash -Eq and ==, What's the Diff
Bash Script to Remove Directories Based on Modified File Date
How to Add Text After Last Pattern Match Using Ed