Bash 在linux后台运行tcpdump

Bash 在linux后台运行tcpdump,bash,stdout,io-redirection,stderr,tcpdump,Bash,Stdout,Io Redirection,Stderr,Tcpdump,Linux(Gentoo)和Linux(AWS免费版上的Redhat) 我是pcap组的成员,可以作为非root用户运行tcpdump 我试图运行一个脚本,在后台运行tcpdump,并将输出发送到文本文件temp.txt。我的脚本将创建一个名为temp.txt的文件,但/usr/bin/tcpdump-tttt不会写入该文件 我可以在没有nohup的情况下运行脚本 /usr/sbin/tcpdump -c 10 -tttt > `pwd`/temp.txt 为什么nohup不起作用?以下

Linux(Gentoo)和Linux(AWS免费版上的Redhat)

我是pcap组的成员,可以作为非root用户运行tcpdump

我试图运行一个脚本,在后台运行tcpdump,并将输出发送到文本文件temp.txt。我的脚本将创建一个名为temp.txt的文件,但
/usr/bin/tcpdump-tttt
不会写入该文件

我可以在没有nohup的情况下运行脚本

/usr/sbin/tcpdump -c 10 -tttt > `pwd`/temp.txt
为什么nohup不起作用?以下是我的脚本:

#!/bin/bash
#tpd-txt.sh
nohup /usr/sbin/tcpdump -c 10 -tttt > `pwd`/temp.txt > /dev/null 2>&1 &
试一试

我假设您希望将标准错误重定向到输出,以便可以在日志中捕获它

下面是bash中输出重定向的快速参考指南

1>filename
 # Redirect stdout to file "filename."
1>>filename
  # Redirect and append stdout to file "filename."
2>filename
  # Redirect stderr to file "filename."
2>>filename
  # Redirect and append stderr to file "filename."
&>filename
  # Redirect both stdout and stderr to file "filename."
2>&1
  # Redirects stderr to stdout.
  # Error messages get sent to the same place as standard output.

在bash手册中:“重定向是按照从左到右的顺序处理的。”因此,您的
/dev/null
替换了
`pwd`/temp.txt
,标准输出最终重定向到
/dev/null
。也许你想写
`pwd`/temp.txt 2>/dev/null
?还有,
`pwd`/
不是多余的吗
>temp.txt 2>/dev/null
应该做完全相同的事情。我添加了
pwd
,因为我也尝试在php脚本中以shell_exec的形式运行它。谢谢,请看@1885,这并不能解释为什么你认为你需要
pwd
。无论您是否使用它,输出文件都会在当前工作目录中结束(无论它在PHP运行时中是什么,如果它是父进程)。如果您希望它们都在同一个日志中,请将顺序颠倒为
./temp.txt 2>&1
,这样您就可以在使stdout指向文件后将stdout复制到stderr;操作从左到右一次进行一个。看到了,非常感谢所有的帖子。这很有教育意义!我更好地理解了事情是如何处理的,而不仅仅是复制和粘贴某人的代码。这在AWS远程服务器上起作用:
nohup/usr/sbin/tcpdump-tttt>/dev/null 2>&1>/temp12.txt&
我想了解>/temp.txt的解释。/code.sh替换/home/user/code.sh do>/temp.txt相同的。/和~/?
之间的区别是什么?当前正在从执行脚本的目录中运行<代码>~是根目录
1>filename
 # Redirect stdout to file "filename."
1>>filename
  # Redirect and append stdout to file "filename."
2>filename
  # Redirect stderr to file "filename."
2>>filename
  # Redirect and append stderr to file "filename."
&>filename
  # Redirect both stdout and stderr to file "filename."
2>&1
  # Redirects stderr to stdout.
  # Error messages get sent to the same place as standard output.