I am killing a process inside a script using kill -9 command. The process gets killed but control is exiting from the script. How do i make the following statements work after the kill command?.
kill -9 `ps -ef | grep /home/myFile | grep -v grep | awk {'print $2'}`
sleep 5
echo Process Stopped
Here both sleep and echo are not working. Can some one help?
if you have pkill installed in your machine, you should use it
$ pkill -9 -f /home/myFile
if not, may be you can use the ancient trick instead of grep -v grep
$ kill -9 $(ps -ef | grep '[/]home/myFile' | awk {'print $2'})
The trick is [/]home/myFile.
grep'ing [/]home/myFile matches to /home/myFile,
but argument shown in ps does not contain grep /home/myFile.
Related
I need to create bash script to kill all citrix process. and stop killing process when no process found. I tryed this, but is wrong. Can you help me?
check=`ps -ef | grep citrix | grep -v grep | wc -l`
while [ $check -gt 1 ]
do
ps -ef | grep citrix | grep -v grep | awk '{print "kill "$2}' | bash
done
As mentioned in the comments, you can use pkill to kill a process that matches a string.
Its exit status indicates whether it successfully found processes to kill, so you can use that in the while condition.
while pkill citrix
do
sleep 1
done
I am trying to make a bash script that is killing a process and then it's going to do other stuff.
PID=`ps -ef | grep logstash | grep -v "grep" | awk '{print $2}'`
echo $PID
kill -9 $PID
echo "logstash process is stopped"
rm /home/user/test.csv
echo "test.csv is deleted."
rm /home/example.txt
echo "example.txt is deleted."
When I run the script, it kills logstash as exptected but it terminates also my whole script.
I've also tried: kill -9 $(ps aux | grep 'logstash' | awk '{print $2}').
With this command, my script will be terminated as well.
it looks like your script name includes "logstash".
As a consequence, PID is filled with 2 values, and the kill command kills your script as well.
Rename your script without "logstash" in the name should fix the issue.
This should correct your issue :
PID=$( ps -ef | grep -E '[ ]logstash[ ]' | grep -v "grep" | head -1 | awk '{print $2}')
echo $PID
kill -9 $PID
echo "logstash process is stopped"
rm /home/user/test.csv
echo "test.csv is deleted."
rm /home/example.txt
echo "example.txt is deleted."
Regards!
I wrote a shell script for restart celery. My os is Ubuntu 16.04.3.
I try to run this shell script, just killed celery processes, but not start
a new celery processes. Why?
This is my code.
ps -ef | grep celery | awk '{print $2}' | grep -v grep |xargs kill -9;
celery -A loan_app.tasks worker --loglevel=info --workdir=`pwd` --logfile=/tmp/celery.log --pidfile=/var/run/celery_pid -D
From official documentation:
http://docs.celeryproject.org/en/latest/userguide/workers.html
Please read the text highlighted in bold.
Stopping the worker
Shutdown should be accomplished using the TERM signal.
When shutdown is initiated the worker will finish all currently executing tasks before it actually terminates. If these tasks are important, you should wait for it to finish before doing anything drastic, like sending the KILL signal.
If the worker won’t shutdown after considerate time, for being stuck in an infinite-loop or similar, you can use the KILL signal to force terminate the worker: but be aware that currently executing tasks will be lost (i.e., unless the tasks have the acks_late option set).
Also as processes can’t override the KILL signal, the worker will not be able to reap its children; make sure to do so manually. This command usually does the trick:
$ pkill -9 -f 'celery worker'
If you don’t have the pkill command on your system, you can use the slightly longer version:
$ ps auxww | grep 'celery worker' | awk '{print $2}' | xargs kill -9
There is a miskate in following command.
ps -ef | grep celery | awk '{print $2}' | grep -v grep |xargs kill -9;
It should be as following
ps -ef | grep celery | grep -v grep | awk '{print $2}' |xargs kill -9;
You are excluding "grep" process after taking just process ids and you can never find grep process id
Hope this helps
Just simply use like below it will solve your issue:-
IF you just want to stop all the instances of process celery use below command
ps -ef | grep [c]elery |awk '{print $2}' | xargs kill -9
If you want to stop all the instances of process celery and re-start it again use below command
ps -ef | grep [c]elery |awk '{print $2}' | xargs kill -9
pids=$(ps -ef | grep [c]elery |awk '{print $2}')
if [ "$pids" != "" ]; then
PWD=$(pwd)
celery -A loan_app.tasks worker --loglevel=info --workdir=$PWD --logfile=/tmp/celery.log --pidfile=/var/run/celery_pid -D
else
echo "Failed to kill process celery"
fi
Straight forward example:-
ps -ef | grep [c]elery |awk '{print $2}' | xargs kill -9
PWD=$(pwd)
celery -A loan_app.tasks worker --loglevel=info --workdir=$PWD --logfile=/tmp/celery.log --pidfile=/var/run/celery_pid -D
I'm trying to execute the following command:
ps aux | grep com.scheduler.app.workermain | kill -15 [pid]
How can I obtain the [pid] (or list of PID) using ps aux | grep "expression" and pipe that to kill? There may be zero or many processes running the machine. This is part of an automated job, to ensure all the processes spun will be terminated.
A sample line from the command line, when ps aux | grep com.scheduler.app.workermain is executed is:
jenkins 12373 1.1 4.2 2905440 173628 ? Sl 19:28 0:05 java -Xmx600m -Dlog4j.configurationFile=log4j2-trace.xml -Dpid=foobar -Dipaddr=127.0.0.1 -cp build/classes:build/dependency/* com.scheduler.app.workermain testing.properties
pkill is used for exactly this purpose. How about:
pkill -15 -f com.scheduler.app.workermain
Also if you just want to grep for a PID you can use pgrep:
pgrep -f com.scheduler.app.workermain
pkill man page
kill -15 $(ps aux | grep -i com.scheduler.app.workermain | awk -F' ' '{ print $2 }')
One of possible solutions is to use the pidof command:
kill $( pidof com.scheduler.app.workermain )
PS. You don't need to pass -15 (or -TERM) to the kill command, as SIGTERM is the default signal sent.
Say I want to kill every process containing the word amarok. I can print out the commands I want to execute. But how do I actually make the shell execute them. ie.
ps aux | grep -ie amarok | awk '{print "kill -9 " $2}'
Output:
kill -9 3052
kill -9 3071
kill -9 3076
kill -9 3077
kill -9 3079
kill -9 3080
kill -9 3082
kill -9 3083
kill -9 3084
kill -9 3085
kill -9 3086
kill -9 3087
kill -9 3088
kill -9 3089
kill -9 4031
From man 1 pkill
-f The pattern is normally only matched against the process name.
When -f is set, the full command line is used.
Which means, for example, if we see these lines in ps aux:
apache 24268 0.0 2.6 388152 27116 ? S Jun13 0:10 /usr/sbin/httpd
apache 24272 0.0 2.6 387944 27104 ? S Jun13 0:09 /usr/sbin/httpd
apache 24319 0.0 2.6 387884 27316 ? S Jun15 0:04 /usr/sbin/httpd
We can kill them all using the pkill -f option:
pkill -f httpd
ps aux | grep -ie amarok | awk '{print $2}' | xargs kill -9
xargs(1): xargs -- construct argument list(s) and execute utility. Helpful when you want to pipe in arguments to something like kill or ls or so on.
use pgrep
kill -9 $(pgrep amarok)
The safe way to do this is:
pkill -f amarok
I think this command killall is exactly what you need.
The command is described as "kill processes by name".It's easy to use.For example
killall chrome
This command will kill all process of Chrome.Here is a link about killall command
http://linux.about.com/library/cmd/blcmdl1_killall.htm
Hope this command could help you.
pkill -x matches the process name exactly.
pkill -x amarok
pkill -f is similar but allows a regular expression pattern.
Note that pkill with no other parameters (e.g. -x, -f) will allow partial matches on process names. So "pkill amarok" would kill amarok, amarokBanana, bananaamarok, etc.
I wish -x was the default behavior!
try kill -s 9 `ps -ef |grep "Nov 11" |grep -v grep | awk '{print $2}'` To kill processes of November 11
or
kill -s 9 `ps -ef |grep amarok|grep -v grep | awk '{print $2}'`
To kill processes that contain the word amarok
If you want to execute the output of a command, you can put it inside $(...), however for your specific task take a look at the killall and pkill commands.
You can also evaluate your output as a sub-process, by surrounding everything with back ticks or with putting it inside $():
`ps aux | grep -ie amarok | awk '{print "kill -9 " $2}'`
$(ps aux | grep -ie amarok | awk '{print "kill -9 " $2}')
Maybe adding the commands to executable file, setting +x permission and then executing?
ps aux | grep -ie amarok | awk '{print "kill -9 " $2}' > pk;chmod +x pk;./pk;rm pk
If you're using cygwin or some minimal shell that lacks killall you can just use this script:
killall.sh - Kill by process name.
#/bin/bash
ps -W | grep "$1" | awk '{print $1}' | xargs kill --
Usage:
$ killall <process name>