Skip to content

Commit

Permalink
Fix the pid occupied check when use bookkeeper-daemon.sh start or stop (
Browse files Browse the repository at this point in the history
#3113)

Master Issue: #3112

### Motivation

Fix the failed pid occupied check. we'll fail when use bookkeeper-daemon.sh start or stop bookie, after the last time we exit the bookie direct kill or the bookie occurred non-normal exit, then the bin/bookkeeper-bookie.pid are retained, and the pid in the file is occupied by the thread in other progress.

### Changes
Change the pid occupied check from 'kill -0 $pid' to 'ps -p $pid'. The both will return true when the pid is occupied by one progress, but 'kill -0 $pid' will return true and the 'ps -p $pid' will return false when the pid is occupied by one progress's sub thread.

StackOverflow discussion:
https://stackoverflow.com/questions/30958964/why-do-kill-0-pid-echo-and-ps-ppid-echo-sometimes-differ

Co-authored-by: nicklixinyang <[email protected]>
  • Loading branch information
Nicklee007 and nicklixinyang authored Apr 29, 2024
1 parent d7328a5 commit 3ed93a0
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions bin/bookkeeper-daemon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ start()
{
if [ -f $pid_file ]; then
PREVIOUS_PID=$(cat $pid_file)
if kill -0 $PREVIOUS_PID > /dev/null 2>&1; then
if ps -p $PREVIOUS_PID > /dev/null 2>&1; then
echo $command running as process $PREVIOUS_PID. Stop it first.
exit 1
fi
Expand All @@ -125,7 +125,7 @@ start()
echo $! > $pid_file
sleep 1; head $out
sleep 2;
if ! kill -0 $! > /dev/null ; then
if ! ps -p $! > /dev/null ; then
exit 1
fi
}
Expand All @@ -134,13 +134,13 @@ stop()
{
if [ -f $pid_file ]; then
TARGET_PID=$(cat $pid_file)
if kill -0 $TARGET_PID > /dev/null 2>&1; then
if ps -p $TARGET_PID > /dev/null 2>&1; then
echo stopping $command
kill $TARGET_PID

count=0
location=$BOOKIE_LOG_DIR
while kill -0 $TARGET_PID > /dev/null 2>&1;
while ps -p $TARGET_PID > /dev/null 2>&1;
do
echo "Shutdown is in progress... Please wait..."
sleep 1
Expand All @@ -155,7 +155,7 @@ stop()
echo "Shutdown completed."
fi

if kill -0 $TARGET_PID > /dev/null 2>&1; then
if ps -p $TARGET_PID > /dev/null 2>&1; then
fileName=$location/$command.out
# Check for the java to use
if [[ -z ${JAVA_HOME} ]]; then
Expand Down

0 comments on commit 3ed93a0

Please sign in to comment.