LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Scripting: Getting a pid and killing it via a shell script (https://www.linuxquestions.org/questions/programming-9/shell-scripting-getting-a-pid-and-killing-it-via-a-shell-script-257262/)

topcat 11-20-2004 11:40 AM

Shell Scripting: Getting a pid and killing it via a shell script
 
find pid which is in /usr/local/var/slapd.pid

store in variable
kill variable
restart slapd


I am trying to write a shell script to get the process id of a particular process (slapd)
and then kill that using 'kill -9 $pid'.

I can get the pid by using prid= pidof slapd
An echo of this for testing gives me the pid number followed by a blank line and then the prompt.


Now the problem is when I am trying to use this variable in various forms in the shell script and get errors.

option 1:
kill -9 $prid

Output:
20685
kill: usage: kill [-s sigspec | -n signum | -sigspec] [pid | job]... or kill -l [sigspec]

=========================
Option 2:
actual= "kill -9 $prid"
echo $actual

Output:
20685
./testscript: line 4: kill -9 : command not found

==========================

Option 3:
actual="kill -9 $prid"
echo $actual

Output:
20685
kill -9
#The process is still running.

Can you shed some light on where I am going wrong?
Thank you very much. All help appreciated.

perfect_circle 11-20-2004 11:52 AM

maybe you don't get the pid correct.

try
echo $prid
after you store it in the $prid variable to see what the variable contains

jlliagre 11-20-2004 01:23 PM

It is unclear what you are doing wrong, please post the complete scripts you use.

Another advice, it is always a bad idea to use "kill -9" to kill a process, as doesn't give the process a chance to "die" properly, first just use kill without any option (or -15 / -TERM or -2/-INT), then, only if the process doesn't dies, you can use -9/-KILL.

This is specifically what is recommended for slapd in the OpenLDAP administrator's guide (http://www.openldap.org/doc/admin22/...opping%20slapd), where this command is suggested:
Code:

kill -INT `cat /usr/local/var/slapd.pid`
which looks like doing just what you're trying to.

topcat 11-21-2004 12:29 AM

thanks. i was able to stop it regularly
 
hi. thanks for your response.
Yes i could get the pid fine. The echo did return the pid value fine.


Jilliagre:
Thanks for the tip. I should move away from that kill -9 that i use so often.
However, since i posted, i tried to stop LDAP regularly, via
/etc/rc.d/init.d/ldap stop


I am taking the liberty to provide more details on why I am/was doing this.

I am running an email server with the configuration of redhat 9.0, apache, squirrelmail, postfix, ldap, maildrop, courier, spamassassin, clamv etc.

Now because these domain names are very popular, we get hit by Infinite spam as well as smtp requests. Total 50 domains.


a)The infinite spam to randomCharacters@domainname.com leads to ldap constantly checking whether user is valid or not.

b) Multiple smtp requests leads to a number of postfix processes running (cleanup, trivial-rewrite, smtp) that jams the system.

Have since then implemented blacklists, IPtables (blocks IPs if multiple unsuccessful attempts are made.

However still observe that when X postfix processes are running, system slowly dies and even though ldap is running, the load avg., constantly falls and reaches 0.

If I restart ldap at this point, things start working again. Therefore needed a way to restart it via Monit.

while it's a not a perfect way, it may work. Am still in the process of testing that.

If it helps in providing some more advice, my maxproc value in master.cf for postfix is set to 75
should i change it?


thanks!!

jijujin 12-17-2006 02:55 AM

Shell Scripting: Getting a pid and killing it via a shell script
 
this is probably an easier way to do it:

kill $(pidof slapd)

you might consider running amavisd-new and store your 'spam-data' in a MySQL db

wmakowski 12-18-2006 05:48 PM

I wrote this script a while back to cycle setiathome on or off depending on the pid in a file (pid.sah). Just in case it wasn't running and another program took that process number I made sure it was a setiathome process before killing it, that is what the if statement is all about. You should be able to modify this to suit your needs. If you have questions, ask away.

Code:

#!/bin/ksh
#
# Stop or start setiathome depending on current status.
# Will not start process if file work_unit.sah is missing.
#
 
cd /usr/local/setiathome
read pid < pid.sah
pidcom=$(ps -hp $pid -o %c)
es=$?
if [[ $es = 0 && $pidcom = setiathome ]]; then
    kill $pid
elif [[ -a work_unit.sah ]]; then
    ./setiathome -nice 19 > /dev/null 2> /dev/null &
fi

Bill

jlinkels 12-18-2006 08:51 PM

This is NOT fair!

I wrote several scripts with the most intelligent, sophisticated and tricky ways to store, retrieve and or find a pid, including ps, awk and grep, and now here is someone telling me that there is a command "pidof"

SIGH

Why are there so many commands which are only discovered too late?

jlinkels

ghostdog74 12-18-2006 09:46 PM

to add a few more, there are also
pkill, killall, pidofproc .

/bin/bash 12-23-2006 07:35 AM

Quote:

This is NOT fair!

I wrote several scripts with the most intelligent, sophisticated and tricky ways to store, retrieve and or find a pid, including ps, awk and grep, and now here is someone telling me that there is a command "pidof"

SIGH

Why are there so many commands which are only discovered too late?

jlinkels
Ha ha. One day while doing exactly what you just described. I went to test my "pidof" script and typed pidof instead of ./pidof and it worked! It worked even better than I expected! :p Of course I checked and sure enough I was reinventing the wheel, only my wheel was kinda flat. :)

activeco 12-27-2006 10:33 AM

Quote:

Originally Posted by jlinkels
This is NOT fair!
Why are there so many commands which are only discovered too late?

Lol, indeed. Another one here.
Just spent a lot of time finding the best way to achieve it, almost settled with "ps ux | awk '/process-name/ && !/awk/ {print $2}" and finally stumbled upon this thread.

stephen84s 07-15-2007 11:39 AM

pidof
 
I was trying for /sbin/pidof process_name command but it always returned me a blank, however
"ps ux | awk '/process-name/ && !/awk/ {print $2}'" worked for me !!!

Good thing activeco stumbled onto it :D

gnashley 07-15-2007 12:28 PM

kill -INT $(pgrep slapd)
I use the -n and -o options sometimes to find the latest-run instance or a previous copy. You can also filter by user name or group, etc.

I recently came up with an interesting (BASH) way to use kill -INT to create a sort of non-polling daemon which uses no cycles.
usbwatch is the program I want to keep running, but I needed it to update its' data when signalled by another program. By starting it as below it can be restarted by sending -INT to it(directly to usbwatch). This wrapper retains the same pid and the program which inerrupts should find the pid of usbwatch and send it -INT.

Code:

MY_PID=$$

trap_int() {
 sleep .5
 usbwatch &> /dev/null

}

while [ -d /proc/$MY_PID ] ; do
 trap trap_int 2
 # note that leaving out this initial start of the program means that the loop waits for
 # an INT signal before starting the program above.
 usbwatch &> /dev/null
done


chrism01 07-15-2007 08:32 PM

Hi topcat, you do know RH 9 hasn't been in support for yrs now, inc security updates?

/bin/bash 07-16-2007 04:20 AM

Quote:

chrism01
Hi topcat, you do know RH 9 hasn't been in support for yrs now, inc security updates?
Look at the date on the OP.

chrism01 07-16-2007 05:51 AM

OMG, resurrection time ... ;)


All times are GMT -5. The time now is 12:51 AM.