Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-14-2009, 11:42 PM   #1
LQ Newbie
Registered: Jun 2009
Posts: 2

Rep: Reputation: 0
Question How to terminate Bash Script if remote session disconnects

Dear All,
i am using a bash interactive script that waits for user input. i have to use this script from ssh session via remote access. if session drops, script stays stuck and BASH chews up CPU utilization:confused :.
Can any one guide me how a script can self terminate if remote session is dropped?
Old 06-14-2009, 11:51 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
Try these (you'll have to adapt them a bit)
Old 06-15-2009, 03:45 AM   #3
LQ Newbie
Registered: Jun 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Dear chrism01, thanx for your prompt response. i have tested those scripts, but i am not getting result. When i use my script via ssh, it wait for READ action for input data and if ssh is terminated then it stays stuck. I want to make my script susid if there is no input in READ for say 30 seconds
Old 06-15-2009, 03:55 AM   #4
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,293

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

You can use -t for timeout with the read. For example

echo "You have 30 seconds to write something:"
read -t 30 testvar
if [ "$?" -eq "0" ] ; then
echo "Oki"
echo "Timed out"
Old 06-15-2009, 04:04 AM   #5
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
You can return to the script's session by running it through 'screen'. If youf connection drops the script and shell is maintained for you to re-attach once you ssh back in.

'man screen'


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh session within a bash script Tamaros Programming 1 05-11-2008 03:38 AM
bash - start remote script from local script? babag Programming 7 04-06-2008 05:46 PM
terminate session after 15 minutes jonfa Linux - Security 1 06-22-2007 11:25 AM
how to have bash script check whether it's a x session? bkeeper Linux - Newbie 9 02-21-2006 06:31 AM
Terminate Session Remotely wwnexc Linux - Software 13 01-06-2006 06:02 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:40 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration