Killing Process in Shell Script

Shell script to capture Process ID and kill it if exist

Actually the easiest way to do that would be to pass kill arguments like below:

ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill

Hope it helps.

How to write a shell script to find PID and Kill

When you run

ps -eaf | grep "start.sh" | awk '{print $2}'

you create a subshell with the word start.sh in it. grep will then pick up on its own process and the start.sh one so you will get two PIDs back.

This means when you are trying to kill both start.sh and the

ps -eaf | grep "start.sh" | awk '{print $2}'

processes. The start.sh will die but the other will no longer exist so can't be killed, so it gives you an error.

If you were to split up the commands you might have better luck:

PIDS=$(ps -eaf)
PID=$(echo "$PIDS" | grep "start.sh" | awk '{print $2}')

Shell script to kill a process in specific time with process name and time as input

  • With this script you can kill the running process at a specific time by giving the process name and the time.

    [Note: The input for time must be in seconds only, i.e 120 for 2 minutes]
#!/bin/bash

LOG=/tmp/kill.log
EXIT_ON_KILL=true
read -p 'Process: ' name
read -p 'killat: ' time

PID=$(ps -ef | grep $name | awk '{print $2}')
ps -ef | grep $name | awk '{print $2}' &>>$LOG

if [ $? -eq 0 ]; then
echo -e "\n The process details "
ps -p $PID
else
echo -e "\nInvalid Process Name"
fi

current=$(date +"%T")
killat=$(date -d "+"$time" seconds" "+%T")
echo -e "\nCurrent time $current \nThe time target is $killat"

while :
do
current=$(date +"%T")
echo $current

if [ "${killat}" == "${current}" ]
then
kill -9 $PID &>>$LOG
if [ $? -eq 0 ]; then
echo "Process $name have been successfully killed"
if [ $EXIT_ON_KILL == true ];then
exit 0
fi
else
echo -e "\nFailed to Kill process $name"
echo -e "\nMay be Invalid Process Name"
exit 1
fi

fi
sleep 2
done

Sample Input:

Process: xxxx
Killat: 120

Killing a process in cmd or shell using a script

On window you can make a .bat file with this content:

TASKKILL /F /IM "program_name.exe"

replace program_name with the program you want to kill. Here's a decent article on TASKKILL

Or if it's just tomcat you can run shutdown.bat... if TOMCAT_HOME isn't set just run it from the tomcat home directory.

kill a process in bash

To interrupt it, you can try pressing ctrl c to send a SIGINT. If it doesn't stop it, you may try to kill it using kill -9 <pid>, which sends a SIGKILL. The latter can't be ignored/intercepted by the process itself (the one being killed).

To move the active process to background, you can press ctrl z. The process is sent to background and you get back to the shell prompt. Use the fg command to do the opposite.

Find and kill a process in one line using bash and regex

In bash, you should be able to do:

kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}')

Details on its workings are as follows:

  • The ps gives you the list of all the processes.
  • The grep filters that based on your search string, [p] is a trick to stop you picking up the actual grep process itself.
  • The awk just gives you the second field of each line, which is the PID.
  • The $(x) construct means to execute x then take its output and put it on the command line. The output of that ps pipeline inside that construct above is the list of process IDs so you end up with a command like kill 1234 1122 7654.

Here's a transcript showing it in action:

pax> sleep 3600 &
[1] 2225
pax> sleep 3600 &
[2] 2226
pax> sleep 3600 &
[3] 2227
pax> sleep 3600 &
[4] 2228
pax> sleep 3600 &
[5] 2229
pax> kill $(ps aux | grep '[s]leep' | awk '{print $2}')
[5]+ Terminated sleep 3600
[1] Terminated sleep 3600
[2] Terminated sleep 3600
[3]- Terminated sleep 3600
[4]+ Terminated sleep 3600

and you can see it terminating all the sleepers.


Explaining the grep '[p]ython csp_build.py' bit in a bit more detail:

When you do sleep 3600 & followed by ps -ef | grep sleep, you tend to get two processes with sleep in it, the sleep 3600 and the grep sleep (because they both have sleep in them, that's not rocket science).

However, ps -ef | grep '[s]leep' won't create a process with sleep in it, it instead creates grep '[s]leep' and here's the tricky bit: the grep doesn't find it because it's looking for the regular expression "any character from the character class [s] (which is s) followed by leep.

In other words, it's looking for sleep but the grep process is grep '[s]leep' which doesn't have sleep in it.

When I was shown this (by someone here on SO), I immediately started using it because

  • it's one less process than adding | grep -v grep; and
  • it's elegant and sneaky, a rare combination :-)

how to kill a single background process in bash

It looks like the problem might be related to how monitor.sh is called. If I have a script 'foo.sh' that just has a shell command in it, I won't see it identified as 'foo.sh' in a ps listing, but if I call it with sh (or bash) then I do. It seems that if 'foo.sh' is a list of shell commands, the shell will change that to bash , and you won't see 'foo.sh' in the ps listing, but if I explicitly call it with bash, i.e.

bash foo.sh

then I see it in the ps listing.

However, best practice for shell scripts, is to start off with the appropriate 'hashbang' command, i.e. for a bash script the first line should be

#!/bin/bash

this also seems to fix the problem for me. I'm guessing that this line may be missing from monitor.sh, and that's why you don't see it using ps or killall. Once that's in place, you should be able to do

killall -9 monitor.sh

or similar and it will work. Either that or invoke it as bash monitor.sh, but common best practice is to include that first line either way.

cf - http://www.unix.com/shell-programming-and-scripting/21446-ps-command-does-not-display-shell-script-running.html

Killing processes SHELL

The easiest way to kill all commands with a given name is to use killall:

killall php

Note, this only sends an interrupt signal. This should be enough if the processes are behaving. If they're not dying from that, you can forcibly kill them using

killall -9 php


Related Topics



Leave a reply



Submit