Ssh – How to get ssh to run in the background without using -f

shsshunix

I'm attempting to create an ssh tunnel, spin it off into a background process, and write a pid file which I can later use to kill it. I CAN run ssh with -f, but then I don't have a handle on its PID.

However, when I attempt to run the script below via nohup or setsid, control is never returned to the shell from which I attempt to daemonize the script.

#!/bin/sh                                                 

while getopts ":v" flag; do
    case $flag in
        v|verbose) verbose=1;;
        :) echo "option -$lastflag requires an argument"; exit 1;;
    esac 
    lastflag="$flag"
    shift $((OPTIND - 1)); OPTIND=1
done

ssh  -p '2222'  -nNL '5984:localhost:5984' 'blah@foo.com' &
pid=$!
sleep 2;
ps -p $pid >/dev/null 2>&1 || exit 1
echo $pid > "var/couchdb-tunnel.pid"
wait

Invocation:

nohup script.sh
setsid script.sh

How I do convince ssh (I assume) to let go of the terminal? Is there some other way I can accomplish what I want?

Best Answer

The shell builtin you're looking for is disown, which re-parents backgrounded jobs to init so that the script can exit leaving them safely in the background. You probably want to redirect output to /dev/null (or a log file) in order to avoid getting output in the shell after starting the script.

ssh hostname 'command' &>/dev/null &
disown