With all due respect to the users pgrep , pkill , ps | awk ps | awk etc. there is a much better way.
ps -aux | grep ... mind that if you rely on ps -aux | grep ... ps -aux | grep ... to find the process, you run the risk. You may have a use case where this is unlikely, but usually this is not the way to go.
SSH provides a mechanism for managing and controlling background processes. But, like many things SSH, this is an "advanced" function, and many people (it seems from the other answers here) are not aware of its existence.
In my own case, I have a workstation at home on which I want to leave a tunnel that connects to the HTTP proxy server on my officeโs internal network, and another one that gives me quick access to the management interfaces on co local servers. Here's how you could create basic tunnels initiated from home:
$ ssh -fNT -L8888:proxyhost:8888 -R22222:localhost:22 officefirewall $ ssh -fNT -L4431:www1:443 -L4432:www2:443 colocatedserver
They invoke ssh for the background, leaving the tunnels open. But if the tunnel goes away, I'm stuck, and if I want to find it, I have to analyze the list of processes and the house. I have the "correct" ssh (if I accidentally ran several of them that look similar).
Instead, if I want to manage multiple connections, I use the SSH ControlMaster configuration option, as well as the -O command-line option for management. For example, with the following in the ~/.ssh/config file,
host officefirewall colocatedserver ControlMaster auto ControlPath ~/.ssh/cm_sockets/%r@%h:%p
the ssh commands above, when they run, leave spoor in ~/.ssh/cm_sockets/ , which can then provide control access, for example:
$ ssh -O check officefirewall Master running (pid=23980) $ ssh -O exit officefirewall Exit request sent. $ ssh -O check officefirewall Control socket connect(/home/ghoti/.ssh/cm_socket/ghoti@192.0.2.5:22): No such file or directory
And at this point the tunnel (and SSH session management) disappeared without the need to use a hammer ( kill , killall , pkill , etc.).
Returning this to your use case ...
You set up the tunnel through which you want syngergyc talk to syngergys on TCP port 12345. For this, I would do something like the following.
Add an entry to your ~/.ssh/config file:
Host otherHosttunnel HostName otherHost User otherUser LocalForward 12345 otherHost:12345 RequestTTY no ExitOnForwardFailure yes ControlMaster auto ControlPath ~/.ssh/cm_sockets/%r@%h:%p
Note that the -L command-line option is processed using the LocalForward , and the Control {Master, Path} lines are turned on to make sure that you have control after the tunnel is established.
Then you can change your bash script to something like this:
#!/bin/bash if ! ssh -f -N otherHosttunnel; then echo "ERROR: couldn't start tunnel." >&2 exit 1 else synergyc localhost ssh -O exit otherHosttunnel fi
The -f option enters the tunnel, leaving the socket on ControlPath to close the tunnel later. If ssh fails (which may be due to a network error or ExitOnForwardFailure ), there is no need to exit the tunnel, but if this does not work ( else ), synergyc starts, and then the tunnel closes after it exits.
You can also see if you can use the SSH LocalCommand parameter to start synergyc right of your ssh configuration file.