1

I am trying to execute this command expressvpn connect in a bash script to be executed in terminal. The problem is that sometimes it takes too long to connect and I want to make sure it doesn't take too much. I tried this command

some_command
if [ $? -eq 0 ]; then
    echo OK
else
    echo FAIL
fi

but this is for checking wether the command is executed or not, while I want to make sure that if it is not executed in, say 10 seconds, then the script must stop and start over from the beginning. How do I do that? Here's the full code

#!/bin/bash

expressvpn disconnect 

while (0<1); do
    expressvpn connect smart location
    xdg-open http://link 
    sleep 15 
    xdotool key Control_L+w  
    expressvpn disconnect  
    expressvpn refresh
done

I hope I was clear. Thanks in advance.

MFJOE
  • 31
  • 1
  • 4

2 Answers2

1

I hope you find it useful.

timeout 15s expressvpn connect smart location>/dev/null &
0

You can simply execute this :

 timeout 15s expressvpn connect smart location

 case "$?" in
     0)   echo "OK"      ;;
     124) echo "TIMEOUT" ;;
     *)   echo "FAIL"    ;;
 esac

from man timeout

Exit status:

  • 124 if command times out
  • 125 if timeout itself fails
  • 126 if command is found but cannot be invoked
  • 127 if command cannot be found
  • 137 if command is sent the KILL(9) signal (128+9)
Indent
  • 4,675
  • 1
  • 19
  • 35
  • It works as long as I use one "case", but when I use two of them it gives me this error `/typert: line 25: syntax error near unexpected token `)' ./typert: line 25: ` 124) echo "EXPRESSVPN TIMEOUT" ;;' `. Is that because of that value? 124? I tried the debug and this error happens when executing the second case. So one case it's fine but how can I use two? – MFJOE Oct 24 '17 at 21:23