title: Why doesn't tcpdump run in background?
body: <p>I logged in a virtual machine via ssh and I tried to run a script in background, the script is shown below:</p>
  
  <pre><code>#!/bin/bash
  APP_NAME=`basename $0`
  CFG_FILE=$1
  . $CFG_FILE #just some variables
  CMD=$2
  PID_FILE="$PIDS_DIR/$APP_NAME.pid"
  CUR_LOG_DIR=$LOGS_RUNNING
  
  echo $$ &gt; $PID_FILE
  
  #Main script code
  
  #This script shall be called using the following syntax
  # $ nohup script_name output_dir &amp;
  
  TIMESTAMP=`date +"%Y%m%d%H%M%S"`
  
  CAP_INTERFACE="eth0"
  
  /usr/sbin/tcpdump -nei $CAP_INTERFACE -s 65535 -w file_result
  
  rm $PID_FILE
  
  </code></pre>
  
  <p>The result should be tcpdump running in background, redirecting the command result to file_result.</p>
  
  <p>The script is called with:</p>
  
  <pre><code>nohup $SCRIPT_NAME $CFG_FILE start &amp;
  </code></pre>
  
  <p>And It is stopped calling the STOP_SCRIPT:</p>
  
  <pre><code>##STOP_SCRIPT
  PID_FILE="$PIDS_DIR/$APP_NAME.pid"
  
  if [ -f $PID_FILE ]
  then
    PID=`cat $PID_FILE`
  
    # send SIGTERM to kill all children of $PID
    pkill -TERM -P $PID
  fi
  </code></pre>
  
  <p>When I check the file_result, after running the stop script, It is empty.</p>
  
  <p>What is happening? How can I solve it?</p>
  
  <p>I found this link: <a href="https://it.toolbox.com/question/launching-tcpdump-processes-in-background-using-ssh-060614" rel="nofollow noreferrer">https://it.toolbox.com/question/launching-tcpdump-processes-in-background-using-ssh-060614</a></p>
  
  <p>The author seems to have faced a similar issue. They debate about race conditions, but I didn't understand completely.</p>