Ssh – Running ssh/netcat in the background

bashnetcatssh

I'm trying to run a network throughput test between an OS X and a Linux host. I'd like to be able to measure the sustained throughput from the Linux host to the OS X host over time. I'd like to wrap this up into a single script so that I can easily run it as I make changes to the network.

My approach so far has been to start netcat on the OS X host listening on a port (nc -l -k 20000 > /dev/null). Then I'll start netcat on the Linux host and have it send data to the OS X host (ssh <Linux host> 'dd if=/dev/zero | nc <OS X host> 20000 2>&1'). After that I'll run netstat -i 1 to measure the throughput and log it to a file.

This all works great when I open 3 terminal windows and run these commands in the foreground. However, when I try to run either of the netcat commands in the background, the ssh process exits. I've tried using & and -f to background the ssh process.

Here is what the full script looks like so far:

#!/usr/bin/env bash

set -o errexit

nc -l -k 20000 > /dev/null &
LISTEN_PID=$!

ssh <linux host> 'dd if=/dev/zero | nc <os x host> 20000 2>&1' &
SEND_PID=$!

echo "Listen: ${LISTEN_PID}, Send: ${SEND_PID}"

netstat -i 1

Best Answer

You probably just need to use nohup

http://linux.die.net/man/1/nohup