Skip to content

Instantly share code, notes, and snippets.

@ericwastaken
Last active December 21, 2018 05:38
Show Gist options
  • Select an option

  • Save ericwastaken/f3456eefb0571da6a3cdf4636843a587 to your computer and use it in GitHub Desktop.

Select an option

Save ericwastaken/f3456eefb0571da6a3cdf4636843a587 to your computer and use it in GitHub Desktop.
Looks for a pidfile to a previously started SSH background process, then stops the process.
#!/bin/bash
# Get the directory where the script lives.
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
# Setup a variable for easy reference to the pidfile
pidfile=${SCRIPT_DIR}/tunnel.pid
# Check for our pidfile, which we expect in order to be able to control the process
if [ ! -f $pidfile ]; then
echo "Tunnel pidfile does not exist. Tunnel might not up or not known! Exiting!"
exit -1
fi
# Pidfile exists so we keep going...
# Read pidfile
bg_pid=$(<${pidfile})
# Kill process with pid
kill -9 ${bg_pid}
# Remove the pidfile
rm ${pidfile}
# Output
echo "Tunnel process PID=${bg_pid} stopped."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment