Shell script executes fail - linux

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

Related

Execute command up to end

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

.bashrc saves previous process id and does not update in alias commands

I have made an alias in .bashrc to kill my python service.py & process
alias servicestop="kill $(ps -ef | grep -w service.py | grep -v grep | awk '{print $2}')"
Whenever I run first time servicestop command it will kill the process.
but again whenever I start process python service.py &, and execute command servicestop it gives an error.
After research, I found following things.
when I run first time python service.py & process. its process id was 512.
and, command servicestop kill that process(512).
Now when I run Second time process python service.py &. its process id was 546.(definitely it will be different).
When I run command servicestop. it will give following error:
-bash: kill: (512) - No such process
That means $(ps -ef | grep -w service.py | grep -v grep | awk '{print $2}') will return the previous pid, which is already killed.
Now please suggest the solution if any possible.
so whenever I want to run servicestop command, I have to run source .bashrc command first, then run servicestop command to make it work.
Please remove the servicestop alias from your .bashrc and add :
servicestop(){
kill $(ps -ef | grep -w service.py | grep -v grep | awk '{print $2}');
}
In a way, functions in .bashrc are "aliases 2.0" : simply better
Better : same function; but with the name of script to kill as parameter :
servicestop(){
kill $(ps -ef | grep -w $1 | grep -v servicestop | awk '{print $2}');
}
Use it like that :
servicestop service.py
servicestop otherSuperService.py

how can I kill a process in a shell script

My bash script has:
ps aux | grep foo.jar | grep -v grep | awk '{print $2}' | xargs kill
However, I get the following when running:
usage: kill [ -s signal | -p ] [ -a ] pid ...
kill -l [ signal ]
Any ideas, how to fix this line?
In general, your command is correct. If a foo.jar process is running, its PID will be passed to kill and (should) terminate.
Since you're getting kill's usage as output, it means you're actually calling kill with no arguments (try just running kill on its own, you'll see the same message). That means that there's no output in the pipeline actually reaching xargs, which in turn means foo.jar is not running.
Try running ps aux | grep foo.jar | grep -v grep and see if you're actually seeing results.
As much as you may enjoy a half dozen pipes in your commands, you may want to look at the pkill command!
DESCRIPTION
The pkill command searches the process table on the running system and signals all processes that match the criteria
given on the command line.
i.e.
pkill foo.jar
Untested and a guess at best (be careful)
kill -9 $(ps -aux | grep foo.jar | grep -v grep | awk '{print $2}')
I re-iterate UNTESTED as I'm not at work and have no access to putty or Unix.
My theory is to send the kill -9 command and get the process id from a sub shell command call.

grepping the PID of a process - Unix

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.

Linux kill - Do not exit program

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.

Resources