Execute command up to end - linux

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

Related

My bash script won't execute commands after kill command

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!

Shell script executes fail

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

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.

Awk not working inside bash script

Im trying to write a bash script and trying to take input from user and executing a kill command to stop a specific tomcat.
...
read user_input
if [ "$user_input" = "2" ]
then
ps -ef | grep "search-tomcat" |awk {'"'"'print $2'"'"'}| xargs kill -9
echo "Search Tomcat Shut Down"
fi
...
I have confirmed that the line
ps -ef | grep "search-tomcat"
works fine in script but:
ps -ef | grep "search-tomcat" |awk {'"'"'print $2'"'"'}
doesnt yield any results in script, but gives desired output in terminal, so there has to be some problem with awk command
xargs can be tricky - Try:
kill -9 $(ps -ef | awk '/search-tomcat/ {print $2}')
If you prefer using xargs then check man page for options for your target OS (i.e. xargs -n.)
Also noting that 'kill -9' is a non-graceful process exit mechanism (i.e. possible file corruption, other strangeness) so I suggest only using as a last resort...
:)

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