我正在尝试使用 nohup 通过 ssh 在远程机器上运行 tcpdump。
[support@sv4-haswell107-bqkp91500107-node-1 ~]$ nohup sudo tcpdump -i lo port 2049 -s0 -w ./test.pcap >/dev/null&
[1] 14286
[support@sv4-haswell107-bqkp91500107-node-1 ~]$ nohup: ignoring input and redirecting stderr to stdout
[support@sv4-haswell107-bqkp91500107-node-1 ~]$
[support@sv4-haswell107-bqkp91500107-node-1 ~]$
[support@sv4-haswell107-bqkp91500107-node-1 ~]$ ps -ef | grep -i tcpdump
root 14286 28830 3 00:30 pts/1 00:00:00 sudo tcpdump -i lo port 2049 -s0 -w ./test.pcap
tcpdump 14292 14286 1 00:30 pts/1 00:00:00 tcpdump -i lo port 2049 -s0 -w ./test.pcap
support 14325 28830 0 00:30 pts/1 00:00:00 grep --color=auto -i tcpdump
但是当我尝试使用 ssh 运行相同的命令时,tcpdump 并没有在后台运行。
[support@sv4-haswell107-bqkp91500107-node-1 ~]$ ssh -tt -p 2222 support@localhost 'nohup sudo tcpdump -i lo port 2049 -s0 -w ./test.pcap >/dev/null&'
Connection to localhost closed.
[support@sv4-haswell107-bqkp91500107-node-1 ~]$ ps -ef | grep -i tcpdump
support 25861 28830 0 00:28 pts/1 00:00:00 grep --color=auto -i tcpdump
答案1
将输出重定向到文件后,tcpdump 在后台启动
ssh -tt -p 2222 support@localhost' /usr/bin/nohup tcpdump -i lo port 2049 -s0 -w ./test.pcap > /tmp/no.log 2>&1 &'