3

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"
Community
  • 1
  • 1
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
  • What is result if `pidof memcache` or `ps -ef | grep "memcache" | awk '{print $2}'`? Looks ok. – keltar Oct 31 '13 at 06:50
  • have you checked http://stackoverflow.com/a/160926/2231893 – abasu Oct 31 '13 at 06:59
  • pidof memcache does not give anything, no error no id. – Suresh Kamrushi Oct 31 '13 at 07:00
  • 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;` – Ashish Oct 31 '13 at 07:16
  • @Ashish: it gives error ": No such file or directory", but when i run ps -ef | grep memcache | grep -v "grep" it gives only one process. Problem with awk... – Suresh Kamrushi Oct 31 '13 at 07:22
  • I had linux mint using its GUI so there is process gnome terminal i executed `ps -ef | grep gnome-terminal | grep -v "grep" | awk '{print $2}'` and got 2803 which is the PID – Ashish Oct 31 '13 at 07:26

5 Answers5

9

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
Ashish
  • 1,856
  • 18
  • 30
3

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.

BlackMamba
  • 10,054
  • 7
  • 44
  • 67
2

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.

jlliagre
  • 29,783
  • 6
  • 61
  • 72
1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0
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
Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32