Redirecting the output of ps command,getting process id and killing that process using shell script - shell

I want to write a shell script to find the running process for a given user and kill the process by getting the respective process ID.
Its like
ps -ef | grep dinesh
After this, i am getting the output as the following
dinesh 19985 19890 0 11:35 pts/552 00:00:00 grep dinesh
Here 19985 is the process ID. I want to kill that process.
How can i achieve this using script?
I have to parse the ps command output and get the process ID
Thanks in advance.

kill `ps -ef | grep dinesh | awk '{ print $2 }'`

What if there is more than one process defined by the string 'dinesh'? What about the grep process itself? This is a more complete answer
ps -ef | grep dinesh | grep -v grep | awk '{print $2}' | xargs kill -9

Related

How to kill running processes by name from the terminal

I want to kill all processes named ruby. How do I do that in one go, without manually doing kill -9 PID for every process?
A simple oneliner should do the trick for you:
ps aux | grep [s]lack | awk '{print $2}' | xargs kill -9
ps aux -> list all processes
grep -> search for a particular process by name or any identifier
awk '{print $2}' -> get the 2nd column entries from the result of grep
xargs kill -9 -> kill the processes.
killall command may solve your problem. Please check the manual for details.

Strip process pid to log_file_name (weird behaviour when running from cron)

I am curious if anyone have similar issue when running scripts from cron. This line of script should copy opentsdb_daemon.log file to opentsdb_daemon_with_pid.log. Currently openTSDB is running on only one PID.
!/bin/sh
cp -f /opt/opentsdb/opentsdb-2.2.0/var/log/opentsdb/opentsdb_daemon.log "/opt/opentsdb/opentsdb-2.2.0/var/log/opentsdb/opentsdb_daemon_pid_$(ps -ef | grep [o]pentsdb | awk '{print $2}').log
It runs ok. File opentsdb_daemon_pid_76079.log is created but when running fron cron it's creating something like this: opentsdb_daemon_pid_63453?63454?76079.log
I have tried to run it from diffrent cron users - with same effect. I would be extremely grateful for any advice.
The command ps -ef | grep [o]pentsdb | awk '{print $2}' should return multiple PID at the time cron run it, what you get is all the PID separated by "?"
The "?" is because \n is not correctly diplayed in the filename
I assume that it is because when cron executes the command, the command appears in the process list, so :
grep [o]pentsdb is also grep by grep [o]pentsdb ;)
You can determine it by the two consecutive PID 63453 and 63454 which are the process lines "cron execute the command xxx" and the child of this process which is the "command xxx"
Maybe a solution could be to add something like :
$(ps -ef | grep [o]pentsdb |grep -Ev "grep|cron" | awk '{print $2}')

Kill command won't work correctly in bash script

I was running an ubuntu console, when I type the following command, all the processes would be perfectly killed.
kill -9 $(ps -ef | grep 'job1/' | grep -v grep| awk '{print $2}')
But when I was trying to use crontab to call a script routinely, things went wrong.
#!/bin/bash
pid=$(ps -ef | grep 'job1/' | grep -v grep | awk '{print $2}')
echo $pid
kill -9 $pid
# the following commands were never executed
sleep 5
/data/job1/tomcat8/bin/startup.sh
The result was just like this:
15432 15438
Killed
It seems to just killed the job, but won't execute the following commands. Any idea?
If you are going to make a script that kills things by PID then you need to be very careful that you kill the right things.
You already have grep -v grep to avoid killing the grep itself, but it seems that you have not put in anything to protect against the script killing itself. Since you know your own PID you could grep -v that, but what if you are 123 and one of the things you want to kill is 1234? Probably safer to go by script name.

ps aux auto close app

I'm trying to setup a task to kill certain server processes when the server gets into a weird state such as when it fails to boot one process, but another process gets keeps running and so not everything boots up. This is mainly a task for development so you can do jake killall to kill all processes associated with this project.
I'm having trouble figuring out how to get the pid after doing: ps aux | grep [p]rocess\ name | {HOW DO I GET THE PID NOW?} and then after getting the ID how do I pass that to kill -9 {PID HERE}
The PID is the second column, so you can do
ps aux | grep [p]rocess\ name | awk '{print $2}'
All together,
my_pid=$(ps aux | grep [p]rocess\ name | awk '{print $2}')
kill -9 $my_pid
You could also you killall <program> or pkill <program> or pgrep <program>

Kill empty process id

In my Makefile, I have the following to kill all my running nginx processes:
killNginx:
sudo kill -9 $(ps -ef | grep [n]ginx | awk '{print $2}')
The problem is that it gives me an error when there are no nginx processes running. Is there a way I can run the kill command only if nginx is running?
EDIT:
Actually I think the issue is the $2 not getting executed. I tried $$2 but still does not work. Anyone have any other ideas to escape $2?
Found a way that made it work for me:
killNginx:
ps -ef | grep [n]ginx | awk '{print $$2}' | xargs sudo kill -9
You need to escape every single $ in your recipe that does not introduce a make variable. So, you need to escape both $$(ps ... and print $$2. You seem to be implying that this isn't your problem: however there's no way this will even come close to working without that.
You can also look into killall. Something like killall -I nginx || true might do it.
Give this a shot:
for PID in $(ps -ef | grep [n]ginx | awk '{ print $2 }'); do
sudo kill -9 $PID
done

Resources