1

I was playing around with creating and killing process groups on linux ( ubuntu )

I am trying to kill a process group and am being driven crazy by the fact that pkill -P $ID works while kill -- -$ID does not . In situations other then this script kill -- -$ID works as expected and kills the process and sub process but in this situation it reports process not found while pkill -P kills the group as expected .

Why is pkill working and kill not .

file="${@:1:1}"
params="${@:2}"

echo $file 
echo $params


while true ; do
  echo +Master $BASHPID

  (
    echo ++Slave $BASHPID

    running=''
    stat=''

          while true; do
            echo RUNNING $running
            echo +++Cycle $BASHPID
            sleep 1
            new_stat="`stat --printf='%Y' "$file"`"
            ! [ "$stat" = "$new_stat" ] && {
# HERE IS MY ISSUE I DO NOT UNDERSTAND WHY kill -- -"$running" reports process not found . Especially when doing ps f clearly shows both the proper hierarchy and that the PID is as expected . 
              kill -0 "$running"   && pkill -TERM -P "$running"
              stat="$new_stat"
              echo CHANGE
              ( /bin/bash "$file" ${params[@]} ) &
              running=$!
            }

          done  &
          wait

    echo Slave/Cycle  clear 
  ) &
  wait

done


trap "-" EXIT

#stat="`ls`"

running the file

#!/bin/bash

echo ++++sub $BASHPID

(
  while true; do 
    echo +++++tick  $BASHPID
    sleep 5
  done 
) &

wait

echo SUB/tick clear
  • 1
    shells use process groups for jobs only when interactive. With kill -- "-$PGID", it's the process group id you pass (that typically corresponds to the pid of the process that started the process group), not any random pid. – Stéphane Chazelas Dec 18 '14 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.