Bash: how to check if a command executed in time - linux

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.

I hope you find it useful.
timeout 15s expressvpn connect smart location>/dev/null &

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)

Related

how to make sure first command finishes and then only execute second command in shell script

how to make sure first command finishes and then only execute second command in shell script
#!/bin/sh
echo "Stopping application"
#command to stop application
echo "Starting application"
#command to start application
In above code, I wanted to make sure that command to stop application is finished properly and then only start the application.
How to handle this.
Please note in my case if application is already stopped then command to stop application takes some random time to complete i.e. 20sec, 30 sec .
So adding sleep is not proper way.
Main moto behind script is to restart application.
Considering fact that if application is allready stopped it doesnt work properly.
If application is running then the script works perfect.
You can use the command return code and a condition to do this.
#!/bin/sh
echo "Stopping application"
#command to stop application
rc=$?
# if the stop command was executed successfuly
if [ $rc == 0 ]; then
echo "Starting application"
#command to start application
else
echo "ERROR - return code: $rc"
fi
There are 'exit codes', try this:
ls
...
echo $?
0
than:
ls non_existing_file
ls: cannot access 'non_existing_file': No such file or directory
echo $?
2
This command echo $? prints exit code of previous command, if it's 0 than it's OK, all non 0 codes means some kind of error which is not OK.

Script to check connection every 5 minutes and write result to file (without ping) in LINUX

I need to check my connection to a spesific port every 5 minutes, currently i can't use ping command, so i need other alternative to do this.I want to execute this command in shell script
Can someone help me to show some example for this case?
port=80
ip=8.8.8.8
checkIntervalSecs=5
timeoutSecs=1
while true ; do
if $(nc -z -v -w$timeoutSecs $ip $port &>/dev/null); then
echo "Server is up!"
else
echo "Server is down!"
fi
sleep $checkIntervalSecs
done
This runs until you kill it. For an explanation of the nc command, it is basically taken from SO question #IporSircer suggested.

Set timeout for shell script, to make it exit(0) when time is over

When I set up a Jenkins job and found a problem about timeout for shell script.
It works like this:
Start Jenkins → control.sh is launched → test1.sh is launched in control.sh
Part code of control.sh is like:
#!/bin/sh
source func.sh
export TIMEOUT=30
# set timeout as 30s for test1.sh
( ( sleep $TIMEOUT && function_Timeout ) & ./test1.sh )
# this line of code is in a = loop actually
# it will launch test2.sh, test3.sh... one by one
# later, I want to set 30s time out for each of them.
function_Timeout() {
if [ ! -f test1_result_file]: then
killall test1.sh
# the test1_result_file will not
# be created if test1.sh is not finished executing.
fi
}
part of func.sh is as below
#!/bin/sh
function trap_fun() {
TRAP_CODE=$?
{ if [ $TRAP_CODE -ne 0 ]; then
echo "test aborted"
else
echo "test completed"
} 2>/dev/null
trap "trap_fun" EXIT
After control.sh is launched by Jenkins job, the whole control.sh will be terminated when time is over, and the line of killall test1.sh is reached, and the Jenkins job stop and fail.
I guess it's because test1.sh is killed and exit code is not 0, so it cause this problem.
So my question is, is there someway to terminate or end the sub-script (launched by the main one, like control.sh in my case) exit with code 0?
Updated on July 1:
Thanks for the answers so far, I tried #Leon's suggestion, but I found the code 124 sent by timeout's kill action, is still caught by the trap code - trap "trap_fun" EXIT, which is in func.sh.
I added more details. I did a lot google job but still not found a proper way to resolve this problem:(
Thanks for your kind help!
Use the timeout utility from coreutils:
#!/bin/sh
timeout 30 ./test1.sh
status=$?
if [ $status -eq 124 ] #timed out
then
exit 0
fi
exit $status
Note that this is slightly different from your version of timeout handling, where all running instances of test1.sh are being terminated if any one of them times out.
I resolved this problem finally, I added the code below in each testX.sh.
trap 'exit 0' SIGTERM SIGHUP
It is to make test1.sh exit normally after it receives killall signal.
Thanks to all the help!

Bash Script Terminate a command and continue

I have a script that after a while executes a command that cannot stop unless you terminate it. How do I stop the command and continue my script?
Let's say I run apt-get update and I want to stop it in a N period of time and then continue my script.
This is an example of a sh script that make use of the "timeout" command to:
Run "sleep 30", timeout on 10 seconds, then kill "TERM", wait 3s, then kill -9
TIMEOUT_TO_SIGNAL=10
SIGNAL_AFTER_TIMEOUT=TERM
WAIT_FOR_KILL="3s"
COMMAND_TO_EXEC="sleep 30"
echo "Run \"$COMMAND_TO_EXEC\", timeout on $TIMEOUT_TO_SIGNAL seconds, then kill \"$SIGNAL_AFTER_TIMEOUT\", wait $WAIT_FOR_KILL, then kill -9"
timeout --signal=$SIGNAL_AFTER_TIMEOUT --kill-after=$WAIT_FOR_KILL $TIMEOUT_TO_SIGNAL $COMMAND_TO_EXEC
if [ $? -eq 124 ]
then
echo "The command $COMMAND_TO_EXEC timed out"
else
echo "The command $COMMAND_TO_EXEC executed without timeout"
fi

Write a bash script to check if process is responding in x seconds?

How can I write a script to check if a process is taking a number of seconds to respond, and if over that number kill it?
I've tried the timeout command, but the problem is it is a source dedicated sever, and when i edit it's bash script:
HL=./srcds_linux
echo "Using default binary: $HL"
and change it to timeout 25 ./srcds_linux and run it as root it won't run the server:
ERROR: Source Engine binary '' not found, exiting
So assuming that I can't edit the servers bash script, is there a way to create a script that can check if any program, not executed w/ the script is timing out in x seconds?
It sounds like the problem is that you're changing the script wrong.
If you're looking at this script, the logic basically goes:
HL=./srcds_linux
if ! test -f "$HL"
then
echo "Command not found"
fi
$HL
It sounds like you're trying to set HL="timeout 25 ./srcds_linux". This will cause the file check to fail.
The somewhat more correct way is to change the invocation, not the file to invoke:
HL=./srcds_linux
if ! test -f "$HL
then
echo "Command not found"
fi
timeout 25 $HL
timeout kills the program if it takes too long, though. It doesn't care whether the program is responding to anything, just that it takes longer than 25 seconds doing it.
If the program appears to hang, you could e.g. check whether it stops outputting data for 25 seconds:
your_command_to_start_your_server | while read -t 25 foo; do echo "$foo"; done
echo "The command hasn't said anything for 25 seconds, killing it!"
pkill ./srcds_linux

Resources