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. |
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 |
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` |
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!! |
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 |
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 |
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 |
to add a few more, there are also
pkill, killall, pidofproc . |
Quote:
|
Quote:
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. |
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 |
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=$$ |
Hi topcat, you do know RH 9 hasn't been in support for yrs now, inc security updates?
|
Quote:
|
OMG, resurrection time ... ;)
|
All times are GMT -5. The time now is 12:51 AM. |