-   Linux - Newbie (
-   -   How to terminate Bash Script if remote session disconnects (

zia.hassan 06-15-2009 12:42 AM

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?

chrism01 06-15-2009 12:51 AM

Try these (you'll have to adapt them a bit)

zia.hassan 06-15-2009 04:45 AM

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

Guttorm 06-15-2009 04:55 AM


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"

JulianTosh 06-15-2009 05:04 AM

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'

All times are GMT -5. The time now is 01:07 PM.