title: How do I kill a backgrounded/detached ssh session?
body: <p>I am using the program synergy together with an ssh tunnel</p>
  
  <p>It works, i just have to open an console an type these two commands:</p>
  
  <pre><code>ssh -f -N -L localhost:12345:otherHost:12345 otherUser@OtherHost
  synergyc localhost
  </code></pre>
  
  <p>because im lazy i made an Bash-Script which is run with one mouseclick on an icon: </p>
  
  <pre><code>#!/bin/bash
  ssh -f -N -L localhost:12345:otherHost:12345 otherUser@OtherHost
  synergyc localhost
  </code></pre>
  
  <p>the Bash-Script above works as well, but now i also want to kill synergy and the ssh tunnel via one mouseclick, so i have to save the PIDs of synergy and ssh into file to kill them later:</p>
  
  <pre><code>#!/bin/bash
  
  mkdir -p /tmp/synergyPIDs || exit 1
  rm -f /tmp/synergyPIDs/ssh || exit 1
  rm -f /tmp/synergyPIDs/synergy || exit 1
  
  [ ! -e /tmp/synergyPIDs/ssh ] || exit 1
  [ ! -e /tmp/synergyPIDs/synergy ] || exit 1
  
  ssh -f -N -L localhost:12345:otherHost:12345 otherUser@OtherHost
  echo $! &gt; /tmp/synergyPIDs/ssh
  synergyc localhost
  echo $! &gt; /tmp/synergyPIDs/synergy
  </code></pre>
  
  <p>But the files of this script are empty.</p>
  
  <p>How do I get the PIDs of ssh and synergy?<br>
  (I try to avoid <code>ps aux | grep ... | awk ... | sed ...</code> combinations, there has to be an easier way.)</p>