Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6170369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:01:29+00:00 2026-05-23T23:01:29+00:00

I am a beginner user of linux, and also quite newbie at ssh and

  • 0

I am a beginner user of linux, and also quite newbie at ssh and tunnels.

Anyway, my goal is to maintain a ssh tunnel open in background.

In order to do that, I wrote the following batch that I then added into crontab (the batch is automatically processed every 5 minutes during workdays and from 8am to 9pm).
I read in some other thread in stackoverflow that one should use autossh that will ensure the ssh will always be ok through a recurrent check. So did I….

#!/bin/bash
LOGFILE="/root/Tunnel/logBatchRestart.log"
NOW="$(date +%d/%m/%Y' - '%H:%M)" # date & time of log

if ! ps ax | grep ssh | grep tunnelToto &> /dev/null
then
    echo "[$NOW] ssh tunnel not running : restarting it" >> $LOGFILE
    autossh -f -N -L pppp:tunnelToto:nnnnn nom-prenom@193.xxx.yyy.zzz -p qqqq
    if ! ps ax | grep ssh | grep toto &> /dev/null
    then
            echo "[$NOW] failed starting tunnel" >> $LOGFILE
    else
            echo "[$NOW] restart successfull" >> $LOGFILE
    fi
fi

My problem is that sometimes the tunnel stops working, although every thing looks ok (ps ax | grep ssh > the result shows the two expected tasks : autossh main task and the ssh tunnel itself). I actually know about the problem cause the tunnel is used by a third party software that triggers an error as soon as the tunnel is no more responding.

SO I am wondering how I should improve my batch in order It will be able to check the tunnel and restart it if it happens to be dead. I saw some ideas in there, but it was concluded by the “autossh” hint… which I already use. Thus, I am out of ideas… If any of you have, I’d gladly have a look at them!

Thanks for taking interest in my question, and for your (maybe) suggestions!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:01:29+00:00Added an answer on May 23, 2026 at 11:01 pm

    Instead of checking the ssh process with ps you can do the following trick

    create script, that does the following and add it to your crontab via crontab -e

    #!/bin/sh
    
    REMOTEUSER=username
    REMOTEHOST=remotehost 
    
    SSH_REMOTEPORT=22
    SSH_LOCALPORT=10022
    
    TUNNEL_REMOTEPORT=8080
    TUNNEL_LOCALPORT=8080
    
    createTunnel() {
        /usr/bin/ssh -f -N  -L$SSH_LOCALPORT:$REMOTEHOST:SSH_REMOTEPORT -L$TUNNEL_LOCALPORT:$REMOTEHOST:TUNNEL_REMOTEPORT $REMOTEUSER@$REMOTEHOST
        if [[ $? -eq 0 ]]; then
            echo Tunnel to $REMOTEHOST created successfully
        else
            echo An error occurred creating a tunnel to $REMOTEHOST RC was $?
        fi
    }
    
    ## Run the 'ls' command remotely.  If it returns non-zero, then create a new connection
    /usr/bin/ssh -p $SSH_LOCALPORT $REMOTEUSER@localhost ls >/dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        echo Creating new tunnel connection
        createTunnel
    fi
    

    In fact, this script will open two ports

    • port 22 which will be used to check if the tunnel is still alive
    • port 8080 which is the port you might want to use

    Please check and send me further questions via comments

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a beginner jquery user so I'm really hoping this will be quite straight
I am a beginner SQLite user and has run into some trouble, hoping to
I am beginner in web development, I am developing a site that allows user
I am a beginner in PHP. How can I restrict user access to controller.php
I'm a beginner in C# , I'm trying to do this ..... user input
I'm a beginner user to Visual Studio and I got these annoying lines to
I'm a beginner user of Microsoft Visual Studio 2008 and I want to ask
I am a beginner user of SQL Reporting Services. I wrote a few reports.
I am only a very beginner user of git and have a C# Visual
Hi I am a beginner jQuery user and I am trying to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.