which programs will the killall command kill? - linux

How do I list which programs will be killed with a killall command before I run it?
I'm looking for a killall -dryrun java kind of command, which would list all java processes that would be killed by executing the killall java command.

You can use pgrep as follows to list the processes first:
pgrep -a java

Depending on the killall version used on your system killall -s might do the trick. E.g. this is the case at least on Mac OS X and FreeBSD
On other variants of killall -s SIGNAL is used to specify the signal to send. Killall is not part of UNIX/POSIX standard so implementations vary.

Related

Is there a simple method to kill a process and all subprocess in linux system?

When I want to kill a process using the pid in linux, its subprocess still existes. I hope to kill all process using one command.
Suggesting command pkill -p PID pattern .
See more documentation here.
Check out process groups:
https://en.wikipedia.org/wiki/Process_group
Assuming you want to do this from a shell?
If you do a kill and make the top process negative it does a killpg under the covers and sends the signal to all the processes in the group.

Why does the "kill" command work differently in bash and zsh

the -L flag provided in kill does not work in zsh.
When I run the command kill -L Using zsh the result is:
kill: unknown signal: SIGL
kill: type kill -l for a list of signals
Running kill -L Using bash gives the list of signal names as expected.
-L, --table
List signal names in a nice table.
Please help me understand why this inconsistency, and can it be "fixed"?
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.

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]

linux shell kill signal SIGKILL && KILL

I just written a shell script to control the start and stop of a module. Everything seems normal until I find the stop command result in something unexpected.
I use the command kill -s SIGKILL -- -gpid to kill a group of processes. I use the /bin/sh to run the command like this
/bin/sh -c "kill -s SIGKILL -- -gpid"
which replied the error
/bin/sh: line 0: kill: SIGKILL: invalid signal specification
Then I replaced the /bin/sh with /bin/bash, so the command is
/bin/bash -c "kill -s SIGKILL -- -gpid"
which replied nothing error. so I conclude the explanation that the difference between bash and sh cause the result. However, when I ls the /bin/sh, I found the /bin/sh is a symbolic link to /bin/bash, so the command should be the same.
I found the command syntax kill -s SIGKILL is not in the syntax recommended, kill -s KILL recommended.
so I replaced the SIGKILL with KILL, the command is
/bin/sh -c "kill -s KILL -- -gpid"
which replied nothing error. as described above, anyone could explained this case.
The only truly portable way to write this command is
kill -9 -$gpid
None of the ways to specify a signal name rather than a signal number work on the Unixes that froze their shell utilities in the mid-90s, which is basically all of them except Linux and the open-source BSDs. However, SIGKILL is reliably signal number 9 and has always been so (since V7 if not earlier).
The special argument -- isn't portable either, and is unnecessary in this case.
If you want to be a little more polite about it (sending SIGTERM instead) then use
kill -15 -$gpid
Again, that number is reliable all the way back to V7.
When bash is invoked as sh (e.g. via symlink, as in your case), it uses a sh compatibility mode where most modern features are turned off. I'd bet sh is calling the external binary for kill, and it doesn't recognize SIGKILL, but the bash invocation is using its builtin, and that builtin does.
It's all about bash compatibility. Quick fix to use /bin/bash because sh can't recognize SIGINT or other features.

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