LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-20-2004, 11:40 AM   #1
topcat
Member
 
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62

Rep: Reputation: 15
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.
 
Old 11-20-2004, 11:52 AM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
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
 
Old 11-20-2004, 01:23 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,459

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
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.
 
Old 11-21-2004, 12:29 AM   #4
topcat
Member
 
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62

Original Poster
Rep: Reputation: 15
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!!
 
Old 12-17-2006, 02:55 AM   #5
jijujin
LQ Newbie
 
Registered: Nov 2004
Distribution: Debian
Posts: 4

Rep: Reputation: 0
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

Last edited by jijujin; 12-17-2006 at 03:00 AM.
 
Old 12-18-2006, 05:48 PM   #6
wmakowski
Member
 
Registered: Oct 2003
Location: Ohio
Distribution: Fedora 18, 19, RHL 5.2
Posts: 546

Rep: Reputation: 43
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
 
Old 12-18-2006, 08:51 PM   #7
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 3,980

Rep: Reputation: 476Reputation: 476Reputation: 476Reputation: 476Reputation: 476
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
 
Old 12-18-2006, 09:46 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
to add a few more, there are also
pkill, killall, pidofproc .
 
Old 12-23-2006, 07:35 AM   #9
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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! Of course I checked and sure enough I was reinventing the wheel, only my wheel was kinda flat.
 
Old 12-27-2006, 10:33 AM   #10
activeco
LQ Newbie
 
Registered: Sep 2005
Location: Milky Way
Posts: 14

Rep: Reputation: 0
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.
 
Old 07-15-2007, 11:39 AM   #11
stephen84s
LQ Newbie
 
Registered: Jan 2007
Location: Bombay, India
Distribution: Fedora 7 & 8
Posts: 6

Rep: Reputation: 0
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
 
Old 07-15-2007, 12:28 PM   #12
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,724

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
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
 
Old 07-15-2007, 08:32 PM   #13
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
Hi topcat, you do know RH 9 hasn't been in support for yrs now, inc security updates?
 
Old 07-16-2007, 04:20 AM   #14
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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.
 
Old 07-16-2007, 05:51 AM   #15
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
OMG, resurrection time ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automating the "killing" process via a shell script fatrandy13 Linux - Software 5 09-13-2007 11:59 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM
obtaining PID of shell script sanjith11 Programming 3 07-14-2004 10:43 AM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM


All times are GMT -5. The time now is 03:14 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration