Kill all sleep processes - linux

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]

Related

How does pkill mask work?

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

Does kill command kill processes specific to a path in linux

I have seen many discussions here on kill command. But my confusion is different. I have many processes with the same name and I have to automate the killing. Hence I can't use the pid. So is it possible that if I go to a specific path and use kill <pname> then only the process related to that path will get killed?
Or is there some way to incorporate the path name in kill command?
Instead of using a pid, you could always use the pkill command and have it check against some regular expression. If you pass it the -f flag, it allows you to check against the entire command line rather than just the process name.
Something like this would probably do the trick:
pkill -TERM -u username -f "mwhome.*weblogic\\.NodeManager"
-f is where you would pass in your regex
-u is also useful so that you only affect pid's running as specific users
No, but you when you start the process with
yourcommand & echo $!
or wrap it in a small script
#!/bin/bash
yourcommand &
echo $! >/path/to/pid.file
you can save the pid. And then kill the process with this pid. This is the normal way how to manage the processes. If you look in the normal init.d scripts of perhaps nginx, they do it the same way. Just saving the pid in a file, and at stopping just read the pid and kill the process.

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.

Shell Script for Killing PID

I run a few processes that I created myself on my Ubuntu Server, and to kill them I run:
sudo fuser -n tcp PORT
kill -9 PID-DISPLAYED
Is there any way I can obtain the PID from a port using a shell script, then kill it by running the shell script.
Thanks.
fuser can kill it:
-k, --kill
Kill processes accessing the file. Unless changed
with -SIGNAL, SIGKILL is sent. An fuser process
never kills itself, but may kill other fuser processes.
The effective user ID of the process executing fuser is
set to its real user ID before attempting to kill.
Try using either killall, or pkill, either of which will close all processes of the type of argument you describe, for example:
killall firefox
Will kill all running instances of firefox.
See
this link of pkill.

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