How to kill all open terminals using command lines in CentOs - linux

I'm using putty to connect to Centos and sometimes it disconnect, and the open terminals on centos stays open, is there a way by a command line to close/kill all open terminals?

If you want to kill all open terminals except for the current one, you can use
kill $(pgrep bash)
pgrep bash lists the pids of all the active terminals
if the terminals refuse to die, you can use
kill -9 $(pgrep bash)
the "-9" is used to send the SIGKILL Signal to the process

if you are using linux then just find out the process id of the putty
use ps -a to get PID of process then use kill PID
on Windows:
Open the command prompt as the current user or as Administrator.
Type tasklist to see the list of running processes and their PIDs. ...
To kill a process by its PID, type the command: taskkill /F /PID pid_number.
To kill a process by its name, type the command taskkill /IM "process name" /F.

Identify processes
ps -ef | grep -E 'ssh.*pts' | grep -v grep |awk -F" " '{print $2}'
The above script it will give you the PID of those ssh pst connections on your machine; then as Vikas said you can kill those processes, remember be aware by using the kill command.
NOTE: you can use the last command where you can see a list of the current/olders session on your machine.
Kill processes
kill -9 PID1 PID2 PID3

Kill old login wit command:
pkill -o -u $USER sshd
You can use 'screen' program for reconnect from where your connection was lost.

Related

Linux "kill -9 <PID>" for all processes? [duplicate]

This question already has answers here:
Kill all processes for a given user
(5 answers)
Closed 3 years ago.
I have a bunch of processes on my school's server that have been running for about a week without it being terminated. I found out that I could use "kill -9 [PID]" for each of the PIDs, but it took me awhile to individually kill each of them.
If, for instance, I have hundreds of processes I want to forcefully kill, is there a way to kill them all instantly?
You don't linux has number of commands, use the following with caution, killall or you could try pkill -U UID or pkill -U username
Note when using pkill, it will kill all processes including your tty terminal session if you are using SSH, you will be kicked out!
You can kill process by grep your applicationName. For example
ps aux |grep kpark06 | awk '{print $2}' | xargs sudo kill -9
man kill:
kill [options] [...]
<pid> can be a list. You can put a giant space-separated list of processes after kill, like kill 123 543.
A PID of -1 is special; it indicates all processes except the kill process itself
and init
So, kill -9 -1 will get everything, but that could easily be more than you expect. Having no idea what else is running there, I would only kill all the processes if prepared to restart the server.
If these processes have something in common, you may want killall, which can filter the processes to kill by age, user, and name/context regular expression, as well as asking for confirmation.

Linux cannot kill a PID: invalid signal

IN ubuntu Amazon EC2 instances with root access
when do
ps -e
The process shows up with a valid PID and process name. The database table also suggests the process is still ongoing.
PID TTY TIME CMD
32194 ? 00:00:00 test
32253 ? 00:00:00 mysql
However, any of the following commands kill the process, but returns nothing or "invalid signal".
top
kill
Type in PID
y
returns "invalid signal"
or
kill -9 PID
kill -s PID
etc.
Could any guru enlighten how to deal with the "ghost jobs"?
Did you use the correct rights to kill the process? With root you should be able to kill the process using either:
$ su -
# kill -9 PID
or
$ sudo kill -9 PID
You have the id of the process, say 32194, I suggest you run:
pgrep -l a | grep 32194
If the process name has an 'a', the output will show the line and the process name. if not have an 'a', change the letter by other one.
When the process appear, just kill it with:
pkill <process name>

linux bash script kill process and start it again after kill

Im running several chrome browsers on my computer with different profiles. Profiles are named like "prof1" "prof2" and "prof3". Now I need to make a script which kills specific chrome process and restarts it again.
I cannot use killall command cause I need to be specific which chrome browser I want to kill and if I use kill command script exits after kill command.
I have tried something like this:
#!/bin/bash
kill -9 `ps ax | grep -i prof1 | awk '{print $1}'` &
sleep 2
export DISPLAY=:0.0
/usr/bin/chromium-browser --restore-last-session --user-data-dir=/path/to/prof1/ %U &
This script works nicely but after kill command it exits (saying "Killed") and the browser never gets started again. Kill command does not have any "quiet" option. There is no point of trying 2>&1 cause "Killed" output comes from terminal not from stderr/stdout. I have tried "set -e" and many other things but no luck.
Any help/tips anyone ?
What you can use is pkill's --full/-f flag, which will match the whole command line:
$ sleep 1d &
[1] 23335
$ pkill -f 'sleep 1d'
[1]+ Terminated sleep 1d
And you shouldn't use kill -9.

How to get the process ID to kill a nohup process?

I'm running a nohup process on the server. When I try to kill it my putty console closes instead.
this is how I try to find the process ID:
ps -ef |grep nohup
this is the command to kill
kill -9 1787 787
When using nohup and you put the task in the background, the background operator (&) will give you the PID at the command prompt. If your plan is to manually manage the process, you can save that PID and use it later to kill the process if needed, via kill PID or kill -9 PID (if you need to force kill). Alternatively, you can find the PID later on by ps -ef | grep "command name" and locate the PID from there. Note that nohup keyword/command itself does not appear in the ps output for the command in question.
If you use a script, you could do something like this in the script:
nohup my_command > my.log 2>&1 &
echo $! > save_pid.txt
This will run my_command saving all output into my.log (in a script, $! represents the PID of the last process executed). The 2 is the file descriptor for standard error (stderr) and 2>&1 tells the shell to route standard error output to the standard output (file descriptor 1). It requires &1 so that the shell knows it's a file descriptor in that context instead of just a file named 1. The 2>&1 is needed to capture any error messages that normally are written to standard error into our my.log file (which is coming from standard output). See I/O Redirection for more details on handling I/O redirection with the shell.
If the command sends output on a regular basis, you can check the output occasionally with tail my.log, or if you want to follow it "live" you can use tail -f my.log. Finally, if you need to kill the process, you can do it via:
kill -9 `cat save_pid.txt`
rm save_pid.txt
I am using red hat linux on a VPS server (and via SSH - putty), for me the following worked:
First, you list all the running processes:
ps -ef
Then in the first column you find your user name; I found it the following three times:
One was the SSH connection
The second was an FTP connection
The last one was the nohup process
Then in the second column you can find the PID of the nohup process and you only type:
kill PID
(replacing the PID with the nohup process's PID of course)
And that is it!
I hope this answer will be useful for someone I'm also very new to bash and SSH, but found 95% of the knowledge I need here :)
suppose i am running ruby script in the background with below command
nohup ruby script.rb &
then i can get the pid of above background process by specifying command name. In my case command is ruby.
ps -ef | grep ruby
output
ubuntu 25938 25742 0 05:16 pts/0 00:00:00 ruby test.rb
Now you can easily kill the process by using kill command
kill 25938
jobs -l should give you the pid for the list of nohup processes.
kill (-9) them gently.
;)
You could try
kill -9 `pgrep [command name]`
Suppose you are executing a java program with nohup you can get java process id by
`ps aux | grep java`
output
xxxxx 9643 0.0 0.0 14232 968 pts/2
then you can kill the process by typing
sudo kill 9643
or lets say that you need to kill all the java processes then just use
sudo killall java
this command kills all the java processes. you can use this with process. just give the process name at the end of the command
sudo killall {processName}
If your application always uses the same port, you can kill all the processes in that port like this.
kill -9 $(lsof -t -i:8080)
This works in Ubuntu
Type this to find out the PID
ps aux | grep java
All the running process regarding to java will be shown
In my case is
johnjoe 3315 9.1 4.0 1465240 335728 ? Sl 09:42 3:19 java -jar batch.jar
Now kill it kill -9 3315
The zombie process finally stopped.
when you create a job in nohup it will tell you the process ID !
nohup sh test.sh &
the output will show you the process ID like
25013
you can kill it then :
kill 25013
I started django server with the following command.
nohup manage.py runserver <localhost:port>
This works on CentOS:
:~ ns$netstat -ntlp
:~ ns$kill -9 PID
This works for mi fine on mac
kill -9 `ps -ef | awk '/nohup/{ print \$2 }'`
I often do this way. Try this way :
ps aux | grep script_Name
Here, script_Name could be any script/file run by nohup.
This command gets you a process ID. Then use this command below to kill the script running on nohup.
kill -9 1787 787
Here, 1787 and 787 are Process ID as mentioned in the question as an example.
This should do what was intended in the question.
If you are unaware of the PID, then first find it using TOP command
top -U userid
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
You will get the PID using top, then perform the kill operation.
$ kill -9 <PID>
Today I met the same problem. And since it was a long time ago, I totally forgot which command I used and when. I tried three methods:
Using the STIME shown in ps -ef command. This shows the time you start your process, and it's very likely that you nohup you command just before you close ssh(depends on you) . Unfortunately I don't think the latest command is the command I run using nohup, so this doesn't work for me.
Second is the PPID, also shown in ps -ef command. It means Parent Process ID, the ID of process that creates the process. The ppid is 1 in ubuntu for process that using nohup to run. Then you can use ps --ppid "1" to get the list, and check TIME(the total CPU time your process use) or CMD to find the process's PID.
Use lsof -i:port if the process occupy some ports, and you will get the command. Then just like the answer above, use ps -ef | grep command and you will get the PID.
Once you find the PID of the process, then can use kill pid to terminal the process.
About losing your putty: often the ps ... | awk/grep/perl/... process gets matched, too! So the old school trick is like this
ps -ef | grep -i [n]ohup
That way the regex search doesn't match the regex search process!
if you are on a remote server, check memory usage with top , and find your process and its ID. After that, just execute kill [your process ID] .

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.

Resources