linux - Daemon won't kill children that are reading from a named pipe -


i've written bash daemon keeps eye on named pipe, logs sees on file named $log_file_basename.$date, , creates filtered version of in $actionable_log_file:

while true     date=`date +%y%m%d`     cat $named_pipe | tee -a "$log_file_basename.$date" | grep -p -v "$exceptions" >> "$actionable_log_file" done pkill -p $$  # here it's should kill it's children exit 0 

when daemon running, how process table looks:

/bin/sh the_daemon.sh  \_ cat the_fifo_queue  \_ tee -a log_file.20150807  \_ grep -p -v "regexp" > filtered_log_file 

the problem when kill daemon (sigterm), cat, tee, , grep processes spawned daemon not collected parent. instead, become orphans , keep on waiting input on named pipe.

once fifo receives input, process input instructed , die.

how can make daemon kill children before dying? why aren't dying pkill -p $$?

you want setup signal handler script kills members of process group (its children) in case script gets signalled:

#!/bin/bash  function handle_sigterm() {   pkill -p $$   exit 0 }  trap handle_sigterm sigterm   while true   date=`date +%y%m%d`   cat $named_pipe | tee -a "$log_file_basename.$date" | grep -p -v "$exceptions" >> "$actionable_log_file" done  handle_sigterm  exit 0 

update:

as per pilcrow's comment replace

cat $named_pipe | tee -a "$log_file_basename.$date" | grep -p -v "$exceptions" >> "$actionable_log_file" 

by

cat $named_pipe | tee -a "$log_file_basename.$date" | grep -p -v "$exceptions" >> "$actionable_log_file" & wait $! 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -