I running dbmail under the user dbmail in my Solaris 9 x86 box. I have
this this solution: you can use grep and awk to extract pid numbers and
use a for loop to kill the processes that match. For example:
pidlist=`ps -ef | grep dbmail | grep -v grep | awk '/root/ {print $2}'`
for pid in $pidlist
do
kill -p $pid
done
Let's explain it a little:
The command:
ps -ef | grep dbmail | grep -v grep
generate this output:
dbmail 18991 18987 0 11:02:42 ? 0:00 /usr/lib/dbmail-imapd
dbmail 18999 18987 0 11:02:42 ? 0:00 /usr/lib/dbmail-imapd
dbmail 18982 18981 0 11:02:41 ? 0:00 /usr/lib/dbmail-pop3d
dbmail 18983 18981 0 11:02:41 ? 0:00 /usr/lib/dbmail-pop3d
dbmail 18997 18987 0 11:02:42 ? 0:00 /usr/lib/dbmail-imapd
dbmail 18987 18986 0 11:02:41 ? 0:00 /usr/lib/dbmail-imapd
dbmail 18994 18981 0 11:02:42 ? 0:00 /usr/lib/dbmail-pop3d
dbmail 18992 18981 0 11:02:42 ? 0:00 /usr/lib/dbmail-pop3d
dbmail 18993 18987 0 11:02:42 ? 0:00 /usr/lib/dbmail-imapd
dbmail 18996 18987 0 11:02:42 ? 0:00 /usr/lib/dbmail-imapd
dbmail 18989 18987 0 11:02:41 ? 0:00 /usr/lib/dbmail-imapd
root 18980 1 0 11:02:41 ? 0:00 /usr/lib/dbmail-pop3d
dbmail 18998 18987 0 11:02:42 ? 0:00 /usr/lib/dbmail-imapd
dbmail 18990 18981 0 11:02:41 ? 0:00 /usr/lib/dbmail-pop3d
dbmail 18988 18987 0 11:02:41 ? 0:00 /usr/lib/dbmail-imapd
dbmail 18981 18980 0 11:02:41 ? 0:00 /usr/lib/dbmail-pop3d
dbmail 18995 18987 0 11:02:42 ? 0:00 /usr/lib/dbmail-imapd
root 18986 1 0 11:02:41 ? 0:00 /usr/lib/dbmail-imapd
dbmail 19000 18987 0 11:02:42 ? 0:00 /usr/lib/dbmail-imapd
"grep dbmail" show all process that contains dbmail.
"grep -v grep" quits the line where grep process appears.
The awk command finds the appereance of root and displays the number of
column 2. In this example: 18980 and 18986. Those are the numbers of the
parent processes: So, finally, with the for loop you kill each pid number
that appears in the variable pidlist.
You can issue a single awk command without any grep to to the same, but
it's not as easy to follow. For example:
pidlist=`ps -ef | awk '! /grep/ && /root/ && /dbmail/ {print $2}'`
for pid in $pidlist
do
kill -p $pid
done
This awk display column #2 of all lines that at the same time do not
contain grep and contains root and dbmail. This example is quicker. Maybe
you need to ajust the command to your system ps -ef output.
This is my full rc3.d script. It also uses stunnel to enable ssl if
ENABLESSl=1, but that another topic:
#!/bin/sh
#
# DBMail IMAP/POP3 start/stop script
# May use stunnel to provied SSL IMAP/POP3 support
# OpenSUN Project
# Alejandro Marin, 2003
#
ENABLESSL=0
killdb() {
pidlist=`ps -ef | awk '! /grep/ && /root/ && /dbmail/ {print $2}'`
for pid in $pidlist
do
kill -p $pid
done
}
case "$1" in
'start')
echo "[OpenSUN: Starting DBMail Pop3 Daemon]"
/usr/lib/dbmail-pop3d
echo "[OpenSUN: Starting DBMail Imap Daemon]"
/usr/lib/dbmail-imapd
if [ $ENABLESSL -eq 1 ] ; then {
echo "[OpenSUN: Starting Stunnel Daemon]"
/opt/openssl/sbin/stunnel
}
fi
;;
'stop')
if [ $ENABLESSL -eq 1 ] ; then {
echo "[OpenSUN: Stopping Stunnel Daemon]"
kill `cat /var/run/stunnel.pid`
}
fi
echo "[OpenSUN: Stopping DBMail Pop3/Imap Daemons]"
killdb
;;
*)
echo "Usage: $0 { start | stop }"
;;
esac
exit 0
Alejandro Marin