I have an AS studio project that uses a do shell script command to test a condition. However, the condition sometimes comes back with a result right away, and sometimes it will take over a minute. Is there a way to code it so that if the results takes more than 30 seconds, then just ignore it and move on. I was reading about the with timeout command, but read somewhere that it doesn’t really work with do shell script since do shell script is not an AppleEvent.
Here is some pseudo code of what I am trying to do
on clicked theObject
if the title of the button is equal to “test” then
set results to do shell script ("my script that sometimes take over 1 minute)
if there is no results within 30 seconds, set results to 0
end if
end clicked
which shell command are you using. Some commands like cURL have a timeout function.
Otherwise try to put the shell script exceptionally into an application tell block, which responds to a timeout block.
For example
try
with timeout of 10 seconds
tell application "Finder"
set theResult to do shell script "find / -name hello"
end tell
end timeout
return theResult
on error number n
if n = -1712 then -- Apple Event timed out
return 0
else
-- do further error handling
end if
end try
I am using nc to test to see if a port is alive or not. If it is, it comes back with a result pretty quickly, but if it is not, it waits for the full tcp timeout(about a minute), and I don’t want to have to wait for it to come back for the script to continue especially I am testing a list of servers and ports in my script. I just want it to wait like 10 seconds, if there is no respond, just assume it is down.
nc does have a wait timeout flag, but it does not work. So that’s why I am trying to see if there is an applescript way to get around it.
I am doing something like set results to do shell script (nc -z -w 1 myserver 389)