为什么tcpdump不在后台运行?

问题描述 投票:0回答:1

我通过ssh登录虚拟机,并尝试在后台运行脚本,该脚本如下所示:

#!/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 $$ > $PID_FILE

#Main script code

#This script shall be called using the following syntax
# $ nohup script_name output_dir &

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

结果应该是tcpdump在后台运行,将命令结果重定向到file_result。

脚本被调用为:

nohup $SCRIPT_NAME $CFG_FILE start &

并且它停止了调用STOP_SCRIPT:

##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

运行停止脚本后,当我检查file_result时,它为空。

发生了什么事?我该如何解决?

我找到此链接:https://it.toolbox.com/question/launching-tcpdump-processes-in-background-using-ssh-060614

作者似乎也面临类似的问题。他们辩论比赛条件,但我不完全了解。

linux shell tcpdump nohup
1个回答
0
投票

我不确定您是否要通过继续运行启动脚本本身来尝试完成什么,但是我认为这是一种方法,可以完成您尝试做的事情,即启动tcpdump并让它继续执行通过tcpdump不受挂断的影响。为了说明起见,我已经做了一些简化-随意添加任何您认为合适的变量,例如nohup输出目录,nohup等。>

脚本1:tcpdump_start.sh

#!/ bin / shrm -f nohup.outnohup / usr / sbin / tcpdump -ni eth0 -s 65535 -w file_result.pcap和#将tcpdump的PID写入文件回声$! > /var/run/tcpdump.pid

脚本2:tcpdump_stop.sh

#!/ bin / sh如果[-f /var/run/tcpdump.pid]然后杀死猫/var/run/tcpdump.pidecho tcpdump`cat / var / run / tcpdump.pid`被杀死了。rm -f /var/run/tcpdump.pid其他echo tcpdump未运行。科幻

要启动tcpdump

,只需运行nohup.out。要停止以TIMESTAMP开头的tcpdump实例,只需运行tcpdump_start.sh

捕获的数据包将被写入file_result.pcap

文件,是的,它是一个pcap文件,而不是文本文件,因此有助于使用适当的文件扩展名对其进行命名。当tcpdump终止时,tcpdump统计信息将被写入nohup.out文件。
© www.soinside.com 2019 - 2024. All rights reserved.