无法让脚本在后台运行

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

我希望我的脚本在后台运行,SSH到另一台计算机,运行tcpdump,生成一个pcap文件并将其保存到我的本地计算机。除了后台部分的运行外,我还有所有这些工作。

我已经在Stack Overflow(example)上查看了几个解决方案,但它们似乎对我不起作用。不可否认,我是bash的新手,但是我完全有可能错误地阅读它们。

ssh root@ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" &>/dev/null &disown \ > /root/Destop/BashPcap/01Bash.pcap
linux bash ssh tcpdump
2个回答
1
投票

检查您的报价结尾可能是问题...

或者您可以远程保存文件并使用scp(SecureCoPy)下载。例如:

scp root@ipaddress:/path/to/file ~/Documents/path-where you-want-to-save.pcap


1
投票

据我了解你的任务,这就是你想要的:

nohup ssh root@ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" &> /root/Destop/BashPcap/01Bash.pcap &

简单来说:

nohup - 它将允许您关闭终端,脚本将继续运行

ssh ... - 这是要执行的命令

&> - 将stdout和stderr重定向到文件(Bash 4)

& - 将命令发送到后台

注意:&>将stdout和stderr都发送到文件,如果你想在你的文件中有tcpdump的汇总行,你需要这个。他们被写入stderr:

N packets captured
X packets received by filter
Y packets dropped by kernel

如果您不想拥有这些行,请将stderr发送到/ dev / null

nohup ssh root@ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp"  2>/dev/null > /root/Destop/BashPcap/01Bash.pcap &
© www.soinside.com 2019 - 2024. All rights reserved.