capture pid of tomcat to kill using bash script in Linux - linux

I am trying to write a bash script that finds the PID of tomcat6, kills it, starts it again, then waits 1 minute, then kills the process again and starts it again.
This is what I have so far but I am struggling to have the script kill tomcat using the previous pid:
ps aux | grep tomcat6
kill -9 $!
service Tomcat6 start
sleep 1m
ps aux | grep tomcat6
kill -9 $!
service Tomcat6 start
Thanks

T_PID=$(ps aux | grep Tomcat6 | awk 'NR==1{print $2}')
kill -9 $T_PID
service Tomcat6 start
sleep 1m
If grep gives multiple outputs, NR==x will only get the PID of desired line.

You can just use
pkill tomcat6

To do it only one time it's enough (if you need sudo).
sudo service tomcat6 restart ; sleep 1m ; sudo service tomcat6 restart ;
or even better
sudo /bin/bash -c "service tomcat6 restart; sleep 1m; service tomcat6 restart;"
This can be a never ending loop that you have to interrupt by hands.
Take it like a scratch.
#!/bin/bash
while :
do
pkill tomcat6 # or pkill -9 tomcat6
service tomcat6 start
sleep 1m
done
If I remember well it exists the possibility to restart that service.
If so you can use even
#!/bin/bash
while :
do
service tomcat6 restart
sleep 1m
done

Related

linux kill command -9 vs -15

I have a process that i would like to kill and then restart a service. Someone has written a code to kill the process by writing the following set of scripts
ps -ef |grep "process_name" | awk '{print "kill -15 " $2}'> /projects/test/kill.sh
#run the kill script
/projects/test/kill.sh
and then again
ps -ef |grep "process_name" | awk '{print "kill -9 " $2}'> /projects/test/kill.sh
#run the kill script
/projects/test/kill.sh
#finally
service restart command here
# the problem here is that service does not restart properly sometimes,
as it thinks that process is still running.
As i understand kill -15 gracefully kills the process. But then right away they have the kill -9 as well.
So if a process was getting killed in the first command, what happens when kill -9 is also run on the same process? Or will the ps -ef even list out that process since it has been marked for kill?
Thanks!
You are correct that kill -15 is to gracefully kill a process. But, killing a process is something that happens instantaneously. So the program above is going to check for pid, attempting to kill it gracefully .. If the kill -15 fails -- The kill -9 is performed. The way it knows that kill -15 failed, is the grep command. If kill -15 was successful, that pid should not exist any longer, making the following grep return empty.
So really, kill -9 only runs if kill -15 failed to gracefully stop the program. The problem with this approach, is that sometimes gracefully stopping a process can take some time depending on the program. So IMHO there needs to be a wait period or a sleep for a few seconds to allow kill -15 to attempt to gracefully stop the process .. Most assuredly with the approach above, kill -9 is almost always invoked since the script doesn't allow much time for the process to be shut down properly. In the event that kill -15 is still processing, kill -9 will just override and instantly stop the process.
If you have the option to refactor, you can use /proc/$PID as a more efficient way to detect if a process is running.
stopSvc() { local svc=$1
read x pid x < <( ps -fu "$App_user" | grep -E " ($App_baseDIR/$1/|)$svc.jar$" ||: )
local -i starting="$(date +%s)" # linux epoch timestamp in seconds
while [[ -d "/proc/$pid" ]]
do ps -fp "$pid"
kill -term "$pid"
if (( ( $(date +%s) - starting ) < 20 )) # been trying for less than 20s
then sleep 2
date
else echo "$svc is hung - using a hard stop"
kill -KILL "$pid"
break
fi
done
sleep 2
[[ -d "/proc/$pid" ]] && return 1 || return 0 # flip the return
}
Basically, the kill -15 is a term signal, which the process could catch to trigger a graceful shutdown, closing pipes, sockets, & files, cleaning up temp space, etc, so to be useful it should give some time. The -9 is a kill and can't be caught. It's the Big Hammer that you use to squish the jobs that are misbehaving, and should be reserved for those cases.
You are totally right, this makes little sense. If you're going to use the -9 so soon, might as well skip the careless attempt at better practice and just remove the -15.

Give variable or name to process to kill not every process by this instance, but only with given name(variable)?

I have many processes by one program ( in this case node.js processes) running. Some times i need to run several ( for example 10 nodejs processes) , i start them with Makefile. I want to be able with some bash command within my Makefile to turn off those 10 process when needed, but i dont want to kill other node.js running processes. So i can use pkill node but it will kill every node processes, how can i give some name or some variable for this 10 processes, to kill only them with kill -9 or pkill?
You can store the PIDs of your child processes in a file and use it to kill them later. Example with sleep child processes:
$ cat Makefile
all: start-1 start-2 start-3
start-%:
sleep 100 & echo "$$!" >> pids.txt
kill:
kill -9 $$( cat pids.txt ); rm -f pids.txt
$ make
sleep 100 & echo "$!" >> pids.txt
sleep 100 & echo "$!" >> pids.txt
sleep 100 & echo "$!" >> pids.txt
$ ps
PID TTY TIME CMD
30331 ttys000 0:00.49 -bash
49812 ttys000 0:00.00 sleep 100
49814 ttys000 0:00.00 sleep 100
49816 ttys000 0:00.00 sleep 100
$ make kill
kill -9 $( cat pids.txt ); rm -f pids.txt
$ ps
PID TTY TIME CMD
30331 ttys000 0:00.50 -bash
Note: if you use parallel make you should pay attention to race conditions on pids.txt accesses.
You could try killing the processes by there PID (Process ID):
for example:
# ps -ax | grep nginx
22546 ? Ss 0:00 nginx: master process /usr/sbin/nginx
22953 pts/2 S+ 0:00 grep nginx
29419 ? Ss 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
29420 ? S 1:59 nginx: worker process
29421 ? S 1:54 nginx: worker process
29422 ? S 1:56 nginx: worker process
29423 ? S 1:49 nginx: worker process
29425 ? S 0:09 nginx: cache manager process
30796 ? S 1:49 nginx: worker process
and then you can kill the process with:
kill 22546; kill 22953; kill ...
You can also capture just the PID with:
# ps -ax | grep nginx | cut -d' ' -f1 |
22546
24582
29419
29420
29421
29422
29423
29425
30796
update:
you can write the PIDs to a file and pull them back in make like this:
pids:
echo ps -ax | grep nginx | cut -d' ' -f1 | > PIDs.txt \
FILE="/location/of/PIDs.txt" \
old_IFS=$IFS \
IFS=$'\n' \
lines=($(cat FILE)) \
IFS=$old_IFS \
PID=$(echo {line[4]}) \
kill $PID

How to stop/kill airflow scheduler started in daemon mode

I and new to airflow and accidentally started airflow scheduler in daemon mode. Now, I want to kill the scheduler and possibly restart it. I tried doing
sudo kill -9 <list of pids>
pkill <name>
nothing is happening. When I run
ps aux | grep 'airflow scheduler'
I see these entries:
user1 2907 6.0 1.0 329788 62996 ? Sl 17:37 1:26 /users/user1/anaconda2/bin/python /users/user1/anaconda2/bin/airflow scheduler -D
user1 2909 0.0 0.9 327576 58948 ? Sl 17:37 0:00 /users/user1/anaconda2/bin/python /users/user1/anaconda2/bin/airflow scheduler -D
user1 2910 0.0 0.9 327576 58944 ? Sl 17:37 0:00 /users/user1/anaconda2/bin/python /users/user1/anaconda2/bin/airflow scheduler -D
user1 2911 0.0 0.9 327576 58944 ? Sl 17:37 0:00 /users/user1/anaconda2/bin/python /users/user1/anaconda2/bin/airflow scheduler -D
...and so on for 35 lines with different pids.
Any recommendation as to how I can stop/kill airflow scheduler without restarting my machine. I have also checked the pid file for scheduler and tried killing that pid but no effects.
Any help is appreciated. Thanks!
Unfortuntely
kill $(ps -ef | grep "airflow scheduler" | awk '{print $2}')
I was not able to find a clean solution.
Also looking into the code
https://github.com/apache/incubator-airflow/blob/master/airflow/bin/cli.py
Go to the airflow directory where the pid file is and use:
cat airflow-scheduler.pid | xargs kill
Another alternative is:
/usr/bin/rm -f ${AIRFLOW_HOME}/airflow-scheduler.pid
/usr/bin/pkill -f "airflow-scheduler"
Please note better to remove older daemon .pid file; to avoid any issue while restarting on daemon mode.
You can also see: https://github.com/apache/airflow/issues/77
/bin/sh -c 'PATH=/bin:/sbin:/usr/bin:/usr/sbin mPPID=`cat ${AIRFLOW_HOME}/airflow-scheduler.pid`;ps -o pid= --ppid $mPPID | xargs kill -15 && kill -15 $mPPID && rm -f ${AIRFLOW_HOME}/airflow-scheduler.pid'"
But this command relies on parent PID (.pid file) so if parent process was removed and child processes are still running it won't work.
So in my opinion accepted answer is the best. Or if you have installed use pkill
If using any monitoring service i.e monit like in the github link.
Valid stop command would be:
gPPID=`ps -ef | /usr/bin/grep 'airflow scheduler' | /usr/bin/grep -v grep | awk '{print $2}' `; echo $gPPID; | xargs kill -15 && rm -f ${AIRFLOW_HOME}/airflow-scheduler.pid
cd ~/airflow
cat airflow-scheduler.pid | xargs kill
To kill airflow webserver and scheduler, you can use below command
If you have supervisord configured for airflow, then stop it first
supervisorctl stop all
kill -9 `ps aux | grep airflow | awk '{print $2}'`

To shutdown the apache tomcat 7, which process is the best one killing or shutdown ? and why?

shoutdown : sh shutdown.sh
or
killing tomcat
ps -eaf | grep tomcat
kill -9 number
which one is best?
The best way to shutdown tomact is by using shutdown script. But many people face issues with it and end up doing the second option that you mentioned.
Shutdown script works properly only if the scripts that will be touched during shutdown process are either :
owned by the user.
owned by the PRIMARY group of the user
this will not work if the user belongs in secondary groups.
Some other issues are mentioned here :
https://serverfault.com/questions/115867/tomcat-shudown-does-not-kill-process
In case you have to choose later one, you can use the below mentioned script to make it as a single command. Just put in inside a script and run it whenever you want to kill running tomcat.
#Finds the tomcat process id
ps aux | grep tomcat | grep JavaVirtualMachines | awk -F " " '{print $2}' > tomcatProcessID
#Kills the process id returned from above mentioned command.
kill -9 `cat tomcatProcessID` && tput setaf 3 && echo "Tomcat killed Successfully" ;rm -rf tomcatProcessID
#Not a mandatory command.
#Used to show all the remaining processes with 'tomcat' keyword in it.
#To inform on the console that the tomcat (running ess or indexer) is killed.
ps aux | grep tomcat | grep -v grep | grep -v killtomcat

Command output, pipe, script co-operation

I'm writing a small script to restart my lighttpd server:
1. kill already running process
2. start new server
The script is the following:
PID=$(ps aux | grep lighttpd | grep -v grep | cut -c9-15)
kill $PID
sudo lighttpd -f /etc/lighttpd/lighttpd.conf
My problem is that in a terminal window the command
ps aux | grep lighttpd | grep -v grep | cut -c9-15
gives the result: 11685 but if it runs within the shell script than the result is 11685 13339 13340
What am I missing here?
The ps output of the line containing the lighttpd job is
root 11685 0.0 0.0 11096 1960 ? S 16:40 0:00 lighttpd -f /etc/lighttpd/lighttpd.conf
Why you are wasting time in Linux/unix for grepping PID and killing it when you have killall command
/usr/bin/killall
You can directly
killall lighttpd or /usr/bin/killall lighttpd
if not superuser then use sudo
sudo killall lighttpd or sudo /usr/bin/killall lighttpd
can use preferably -9 with killall like in your case would be sudo killall lighttpd
then restart it via
sudo lighttpd -f /etc/lighttpd/lighttpd.conf
if you are looking for fully automated script then make use of except commands
Click here

Resources