I have a script that runs in the background. It opens an SSH connection to a server and tails a file indefinitely. When I put my mac to sleep, the SSH connection is closed. Here is the script in question:
#!/bin/sh
(ssh <user>@<ip> -o PermitLocalCommand=no \
": > .irssi/fnotify ; tail -f .irssi/fnotify " | \
while read heading message; do \
growlnotify -s -t "${heading}" -m "${message}"; \
done)&
At that point, I have to run ps aux | grep ssh
, find the right process and kill it manually; It's tedious. So I have a couple of questions and potential answers:
- Is there a way to rename the process name of the script so that I can kill it by name everytime I run it again?
- Otherwise, is there a way to store the
pid
of the script when I execute it and then kill it when I run it again?
The first solution is impossible I believe, but it's worth asking.
For the second solution, I did some research I went somewhere but not far enough... Here is what I have:
I managed to get the pid
of the script itself, not the one of the particular SSH connection. But from what I saw, the pid
of the script is always <pid of ssh connection> - 1
.
So I need to add 1 to the pid
of the script, and I couldn't manage to do that. But anyway, is that
a viable solution?
Otherwise, what are the alternative solutions please?