Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

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).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top