I have shutdown all services on the server using putty.How do I find which all services are still running using the grep command? Also how do I kill these services using process id?
You can use below commands:
To list all the running processes:
ps -ef
To list down all processes of a particular user:
ps -u userId
To list a particular process:
ps -ef | grep processName
To list down a particular process run by a particular user:
ps -u userId | grep processName
Now to kill a process:
While you use ps command it list like below:
UID PID PPID C STIME TTY TIME CMD
user1 398 1 0 Mar 08 ? 1331:39 /usr/sbin/nscd
user2 1388 1 0 Mar 08 ? 11:52 /usr/lib/netsvc/yp/ypbind
Now to kill a process you need the process ID that is PID here.
Now to kill a process run by user user1:
kill -9 398 ( sure kill - Not safe)
kill -1 398 ( safe kill - let the process to complete its talk)
kill -15 398 ( safe kill like -1)
kill -8 398 ( Safe kill like -1)
Hope this will help you.
Related
While running ps -ef | grep 'keyword' in cmd i am getting one output values, but while running same command in ansible playbook i am getting two outputs, i dont know why.
Note: process is not running
while running in command, I am getting output like this
user 4917 130211 0 00:20 pts/0 00:00:00 grep --color=auto keyword
while running in ansible playbook i am getting outputs like this
\user 2480 2477 0 07:02 pts/1 00:00:00 grep keyword
user 2477 2476 81 07:02 pts/1 00:00:00 /bin/sh -c ps -ef | grep keyword
When you run ps -ef | grep 'keyword' the resulting list includes the grep command.
Suggesting to use command pgrep
pgrep -af 'keyword'
The output result is a list of lines:
pid 'command line that match RegExp pattern 'keyword'
If interested in pid only:
pgrep -f 'keyword'
If wants to store the pid in variable:
var1=$(pgrep -f 'keyword')
If wants to kill all the process:
pkill -9 -f 'keyword'
I understand the impacts and functions of the kernel processkswapd.
As the output of ps -elf | grep swapd, I found kswapd is started by kthreadd. But how is it started step by step? Where's the extract related source code?
Here is the output of ps -elf | grep swapd:
$ ps -elf | head -n 1; sudo ps -elf | grep -i kswapd
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
1 S root 46 2 0 80 0 - 0 kswapd 11:42 ? 00:00:00 [kswapd0]
You see, the PID of the kernel process kthreadd is 2:
F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD
1 S root 2 0 0 80 0 - 0 kthrea 6/2 00:00:00 [kthreadd]
In addition, I can't find a binary program with the same name throughout the rootfs. For details, see below:
$ cat /proc/46/cmdline
#outputs nothing
sudo find / -iname kswapd 2>/dev/null
#outputs nothing
I think mm/vmscan.c has all or most of the answers you're looking for.
If you're asking how kswapd is initialized, the file contains kswapd_init().
If you're asking how kswapd is woken up by a process that needs more memory, the file contains wakeup_kswapd().
You can use a combination of grep, printk, and dump_stack() commands to step through the instructions executed before and aaft
I am trying to kill a nohup process in an EC2 instance but so far have been unsuccessful. I am trying to grab the process ID (PID) and then use it with the kill command in terminal, like so:
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
with columns, (I believe) they're:
UID PID PPID C STIME TTY TIME CMD
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
However, each time I try to kill the process, I get an error saying that the PID doesn't exist, seemingly because the PID changed. Here is a sequence I am running into in my command line:
// first try, grab the PID and kill
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
[ec2-user#ip-172-31-41-213 ~]$ kill 16580
-bash: kill: (16580) - No such process
// ?? - check for correct PID again, and try to kill again
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16583 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
[ec2-user#ip-172-31-41-213 ~]$ kill 16583
-bash: kill: (16583) - No such process
// try 3rd time, kill 1 PID up
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16584 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
[ec2-user#ip-myip ~]$ kill 16585
-bash: kill: (16585) - No such process
This is quite a struggle for me right now, since I need to kill/restart this nohup process. Any help is appreciated!
EDIT - I tried this approach to killing the process because it was posted as an answer in this thread (Prevent row names to be written to file when using write.csv) and was the 2nd highest rated answer.
Very very bad question ...
You are trying to kill you grep process...
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
The command is grep --color=auto nohup
I'm not sure you can kill nohup
nohup will run your command in a particular way. But after its launching, the nohup process dies.
If you want to grep the ps output :
ps -ef | grep '[n]ohup'
or
pgrep -fl nohup
because you are trying to kill not nohup pid but the grep itself...
I want to kill process through shell scripting it is giving me error. Here is what i tried so far:
When i try to kill memcache it give me error like "kill: No such process" , i used below command:
ps -ef | grep "memcache" | awk '{print $2}' | xargs kill;
or if try like below:
kill -9 $(pidof memcache)
i get error like below:": arguments must be process or job IDs"
When i run directly on command prompt process is running:
ring#ubuntu:~/parasol$ ps aux | grep memcache
memcache 873 0.0 0.0 323220 1188 ? Sl 22:56 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
ring 1714 0.0 0.0 9384 920 pts/0 S+ 23:45 0:00 grep --color=auto memcache
I reff to https://askubuntu.com/questions/239923/shell-script-to-9-kill-based-on-name
AND
Shell script to capture Process ID and kill it if exist
My Ubuntu Version:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
NAME="Ubuntu"
VERSION="12.04.2 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.2 LTS)"
VERSION_ID="12.04"
Acutally
ps -ef | grep memcache
will give two lines..
so you can go this way
ps -ef | grep memcache | grep -v "grep" | awk '{print $2}' | xargs kill;
This can get you exact one PID
if I has to do I would break it in 2 lines in start
#!/bin/bash
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print $2}'`
echo $PID
#to check PID is right
kill -9 $PID
save it in scrip files say test.sh
then on terminal
chmod +x test.sh
then
./test.sh
You can use these commands:
pkill memcached
or
pgrep memcached | xargs kill -9
or
killall memcached.
pgrep, pkill - look up or signal processes based on name and other attributes.
Your first command might be killing itself before having chance to kill the target processes.
For a reliable way, run pkill /usr/bin/memcached or pkill -9 /usr/bin/memcached although the latter is a bad practice.
For the first command, you see that there are two processes that matches the pattern you grep for. The actual memcached process and your grep process. That is probably the reason for the error of the first command line.
Try narrowing the search down, for example by grepping for e.g. "^memcache.*/usr/bin/memcached".
The problem with the second error is that you're calling pidof with the username instead of the process name, so the command is essentially kill -9 without any process id. Try instead e.g. pidof memcached to get the process id of the correct process.
ps -ef | grep "memcache" | awk '{print $2}' | xargs kill;
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print $2}'`
#...
First, you could use cut instead of awk in this case. No need to use a tank to kill a fly ;)
Then, why is it necessary to brutally kill memcache? You have a daemon to stop it :
/etc/init.d/memcached stop
service memcached stop
I'm working on automated deployment using Rake of a mono asp.net website to ubuntu server with nginx.
As far as I've discovered fastcgi-mono-server4.exe can't be stopped gracefully and must be terminated. I currently do that manually in htop. How do I do that from rake or the shell?
I've only a few hours of experience with rake & ruby and a few weeks of linux but made a lot of progress already, however somethings are eluding me even potentially obvious ones like this.
To find the running mono process ids in Linux:
ps ax | grep mono
This will give you something like:
user 1452 0.0 0.0 9396 876 pts/4 S+ 17:33 0:00 grep mono
user 2810 98.2 16.1 967424 330432 ? Sl Mar30 7866:50 /usr/bin/mono /usr/lib/mono/4.0/fastcgi-mono-server4.exe --appconfigdir /etc/init.d/mono-fastcgi /socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.logappconfigdir /etc/init.d/mono-fastcgi /socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.log
The 2nd column contains the process id which you can kill with:
sudo kill -9 2810
Excluding grep from ps output using a character class
Thanks to #Yevgeniy comment, you can exclude grep from the ps output by using a grep character class as explained in this question, e.g:
ps aux | grep [m]ono
Which will exclude the grep process from the ps output and give you something like:
2810 ? Sl 7861:07 /usr/bin/mono /usr/lib/mono/4.0/fastcgi-mono-server4.exe --appconfigdir /etc/init.d/mono-fastcgi /socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.log
This now means if you only have one process of mono running you can now kill it with this 1-liner:
sudo kill -9 $(ps aux | grep '[m]ono' | awk '{print $2}')