2

I am having bash script that checks for ssh connection in lsof. In case it's down, it will open it once again.

I found some information how to create service that starts periodically, but the problem is, that whenever it will start service, and run ssh connection, after exit of service this connection dies.

I know that there is an option RemainAfterExit=yes, but it will remain service active after execution.

So in nutshell:

  1. I want to start bash script periodically
  2. script checks for ssh
  3. If tunnel is down, it will start it (and leave it in system)
  4. Service needs to start once again run script for ssh check

My service configuration:

[Unit]
Description=Periodically check for ssh tunnel
After=network.target

[Service]
Type=simple
Restart=always
RestartSec=30
User=root
ExecStart=/bin/bash /opt/script.sh -s

[Install]
WantedBy=sysinit.target

EDIT:

In my script with -s parameter I am just calling function which is checking if ssh tunnel is established for particular address, and if no, it'll just open it using standard ssh.

if [[ $(sudo lsof -i -n | egrep '\<ssh\>' | grep "$ip" | wc -l) -gt 0 ]]; then
            INFO "Tunnel is established for IP "$ip"."
            exit 0
else
            ERROR "Tunnel for IP "$ip" has been disconnected. Trying to reconnect."
            ssh -fN [email protected]
fi
3
  • 1
    You could also look at autossh to keep ssh open.
    – meuh
    May 21, 2020 at 9:10
  • Please edit your question and add more details about step 3: what does "tunnel down" mean, and how do you start it in your script? Ideally, add the script to your question or at least the relevant parts. I'm not familiar with sshuttle, but if "tunnel down" means the sshuttle process exited and "start it" means start sshuttle, creating a restarting service unit for sshuttle itself would make more sense.
    – danzel
    May 21, 2020 at 9:40
  • @danzel, thanks for message. This sshuttle was not relevant, it was put there by mistake. It is just standard ssh. I am checking for "tunnel down" directly from lsof
    – peanek
    May 21, 2020 at 10:26

0

You must log in to answer this question.

Browse other questions tagged .