How does pkill mask work? - linux

If I have process named raspivid and process named raspivideo running and I execute command pkill raspvid, does it kill both processes or only process which is named raspvid?
If pkill kills both processes is there any linux-command to do the perfect match?

pkill kills all processes that command pgrep lists with the same parameters. In your case it sends SIGTERM signal to both processes.
The following command kills the exactly matching pattern only:
pkill -x raspivid

Related

Execute grep and kill pid in single line in shell script

I need to execute the grep pid and kill -9 command in the same line in shell script as to pass it in expect command but couldn't get it working.
Use pkill PATTERN:
pkill 'jvm'
pkill will send the specified signal (by default SIGTERM) to each process instead of listing them on stdout.

Kill all sleep processes

How do I kill all sleep processes that are running? I realize that I can either use the kill command to kill each process via its PID, or I can use pkill to kill the sleep command by name. I'm trying to figure out how I would do this, any help would be appreciated. I used
man pkill
to get some help but am still unsure.
If you really must do this dangerous action, use the killall command:
killall sleep
There is an option for the pkill command that will kill all processes named the given pattern.
pkill -x [pattern] (or) pkill -exact [pattern]
These commands will kill all processes with the same name as [pattern]

How to kill a shell script and processes it created?

I run a shell script inside php (using shell_exec). I want to kill it and all processes it created when it is in operation. I know the shell script pid. Do you have any suggestion?
I am using ubuntu, php as apache module.
Thank you.
Example:
#!/bin/bash
echo hello
sleep 20
When I kill my script (shell_exec("sudo kill -9 $pid")), the sleep process is not killed which is not desired.
use
pkill -TERM -P pid
will kill the child processes
see this answer
Use this kill command instead:
kill -- -$pid
to kill the running script and all its spawned children.

bash: kill $$ not work?

The command kill $$ should kill current bash, but it seems that it doesn't work:
$ ps -p $$
PID TTY TIME CMD
18179 pts/4 00:00:00 bash
$ kill $$
$ ps -p $$
PID TTY TIME CMD
18179 pts/4 00:00:00 bash
Why?
I'm not sure why one would like to kill the current shell. Nevertheless...
kill PID would send SIGTERM when no signal is specified. bash ignores SIGTERM and SIGQUIT in the absence of any traps.
You'll achieve the desired effect if you were to say
kill -9 $$
or
kill -SIGKILL $$
Quoting from the manual:
When Bash is interactive, in the absence of any traps, it ignores
SIGTERM (so that ‘kill 0’ does not kill an interactive shell), and
SIGINT is caught and handled (so that the wait builtin is
interruptible). When Bash receives a SIGINT, it breaks out of any
executing loops. In all cases, Bash ignores SIGQUIT.
When you send a process any signal using kill command, the process can choose to handle the signal as per its wish. (There are a few signals, which cannot be handled.)
When you use kill $$, you are actually passing it signal=15 (SIGTERM). It can be handled by a process.
You can google for linux signal example to know HOW it is implemented.
& To answer YOUR question:
If you pass an un-handlable signal, like SIGKILL(9) or SIGSTOP(19), it will respectively kill/stop a running bash process.

Killing processes automatically - linux

I al running this command :
pgrep -l someprocess
I get some outputs XXXX someprocess
then I kill every process appearing by hand, I would like to write a script to do it automatically, but this doesn(t make sense
kill -9 $(pgrep -l someprocess | grep "^[0-9]{4}")
someone could help ?
You can use either pkill or killall to accomplish exactly that.
I found this short and clear summary explaining the different ways of killing processes.
pkill is as simple as: pkill someprocess.
#ewm already included a detailed explanation about killall in his answer, so I'm not repeating it here.
You might want to look at the 'killall' command:
KILLALL(1) User Commands KILLALL(1)
NAME
killall - kill processes by name
SYNOPSIS
killall [-Z,--context pattern] [-e,--exact] [-g,--process-group] [-i,--interactive] [-q,--quiet]
[-r,--regexp] [-s,--signal signal] [-u,--user user] [-v,--verbose] [-w,--wait] [-I,--ignore-case]
[-V,--version] [--] name ...
killall -l
killall -V,--version
DESCRIPTION
killall sends a signal to all processes running any of the specified commands. If no signal name is
specified, SIGTERM is sent.
Signals can be specified either by name (e.g. -HUP or -SIGHUP ) or by number (e.g. -1) or by option
-s.
If the command name is not regular expression (option -r) and contains a slash (/), processes execut-
ing that particular file will be selected for killing, independent of their name.
killall returns a zero return code if at least one process has been killed for each listed command,
or no commands were listed and at least one process matched the -u and -Z search criteria. killall
returns non-zero otherwise.
A killall process never kills itself (but may kill other killall processes).

Resources