1

I have a bash script in which I login to a remote machine over ssh and run iperf and then logout and do other things locally. I want iperf to keep running after the bash script logs out. I have tried nohup, disown and setsid, but they don't seem to work when I use them inside the bash script. I have also tried running iperf inside another script, that didn't work either.

Here's the part of the script with nohup example:

ssh root@10.101.10.35 &>/dev/null & << EOF
nohup iperf -s -B 192.168.99.1 &>/dev/null &
EOF
  • Possible duplicate of https://serverfault.com/questions/311593/keeping-a-linux-process-running-after-i-logout – Jaay Jan 26 '18 at 12:51
  • I don't think it is, because what is described in this is he logs in from a terminal and wants to keep it running after logging out. I am able to do this with nohup, disown etc. This is different because I'm running everything inside a bash script and it's not working this way. – Giannis Pappas Jan 26 '18 at 12:55
  • Asked and answered *ad nauseam*. Possible duplicate of [How to make a programme continue to run after log out from ssh?](https://stackoverflow.com/q/954302/608639), [How to prevent a background process from being stopped after closing SSH client in Linux](https://stackoverflow.com/q/285015/608639), [How to keep processes running after ending ssh session?](https://askubuntu.com/q/8653), etc. – jww Jul 31 '19 at 19:54

1 Answers1

2

You need to redirect stdin, stdout and stderr to somewhere else as opposed to your terminal like so:

ssh root@10.101.10.35 'iperf -s -B 192.168.99.1 < /dev/null > /tmp/iperf_combined.log 2>&1 &'

stdin is taken from /dev/null (nothing is entered)

stdout and stderr goes to /tmp/iperf_combined.log

The process will run on the remote machine until you will manually kill it or until the script/command will exit on its own.

Edit (as a reply to the poster's comment):

If you want to run multiple commands in the same ssh session, you may use:

ssh -T root@10.101.10.35 << EOF
iperf -s -B 192.168.99.1 < /dev/null > /tmp/iperf_combined_1.log 2>&1 &
iperf -s -B random_ip2 < /dev/null > /tmp/iperf_combined_2.log 2>&1 &
EOF

As per ssh man page:

-T      Disable pseudo-tty allocation.

Detailed explanation on psqudo-tty here

AnythingIsFine
  • 1,777
  • 13
  • 11